我试图在MATLAB中编写代码,以便将数字舍入为某些(如我所知)有效数字。
我不知道怎么做。有什么建议吗?
答案 0 :(得分:9)
要舍入为d
有效数字:
>> d = 3; %// number of digits
>> x = 5.237234; %// example number
>> D = 10^(d-ceil(log10(x)));
>> y = round(x*D)/D
y =
5.2400
要舍入为d
十进制数字:
>> d = 3; %// number of digits
>> x = 5.237234; %// example number
>> D = 10^d;
>> y = round(x*D)/D
y =
5.2370
修改强>
实际上它更容易:round
功能支持以下选项:
>> d = 3;
>> x = 5.237234;
>> y = round(x, d, 'significant')
y =
5.2400
>> d = 3;
>> x = 5.237234;
>> y = round(x, d) %// or y = round(x, d, 'decimals')
y =
5.2370