% x is the input real number
% k is the number of digits to perform chopping on x
function value = chopk(x,k)

% 檢查輸入的 k 是否為正整數
if k - floor(k) ~= 0 || k < 0
    fprintf('k should be an positive integer \n')
    return
end

% 處理"x = 0"的特殊狀況
if x == 0
    value = 0;
    return
end

% d is the exponent of the scientific decimal expression for x
d = floor(log10(abs(x)));

% shift 將小數點移到要做四捨五入的地方所需要的次數
shift = k-(d+1);

value = fix(x * 10^shift)/10^shift;
