Matlab中分数的任意精度

时间:2015-09-03 11:06:37

标签: matlab

我需要Matlab的一个函数来执行以下操作(我不知道如何用英语解释)

假设:

a = - 1.200000005

我需要一个功能:

f(a) = -1.2; or f(a) = -1.2000

我的意思是,我想设定任意精度。

1 个答案:

答案 0 :(得分:1)

function [output]=ArbAcc(a,digits)
    output = round(a*power(10,digits))/power(10,digits);
end

这会创建一个函数ArbAcc,它会将您的数据相乘,对其进行舍入并最终将其移回以获得所需的输出。

或者,正如@HamtaroWarrior所说,使用roundn(请注意,roundn需要映射工具箱):

a = roundn(a,-digits);

如果我继续阅读文档,我会看到不再推荐使用roundn,请使用round

Y = round(X,N);% rounds to N digits