四舍五入为有效数字 [英] Rounding to n significant digits

查看:155
本文介绍了四舍五入为有效数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在MATLAB中编写代码,将数字整数(如我要求的)有效数字。



我不知道该怎么做。有什么建议?

解决方案

转到 d 有效数字

 >> d = 3; %//位数
>> x = 5.237234; %//示例编号

>> D = 10 ^(d-ceil(log10(x)));
>> y = round(x * D)/ D
y =
5.2400

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' >> d = 3; %//位数
>> x = 5.237234; %//示例编号

>> D = 10 ^ d;
>> y = round(x * D)/ D
y =
5.2370

编辑



其实它更简单:

 >> d = 3; 
>> x = 5.237234;
>> y = round(x,d,'significant')
y =
5.2400

>> d = 3;
>> x = 5.237234;
>> y = round(x,d)%//或y = round(x,d,'decimals')
y =
5.2370


I'm trying to write code in MATLAB that will round number to certain (as I ask) significant digits.

I'm not sure how to do it. Any suggestions?

解决方案

To round to d significant digits:

>> d = 3; %// number of digits
>> x = 5.237234; %// example number

>> D = 10^(d-ceil(log10(x)));
>> y = round(x*D)/D
y =
    5.2400

To round to d decimal digits:

>> d = 3; %// number of digits
>> x = 5.237234; %// example number

>> D = 10^d;
>> y = round(x*D)/D
y =
    5.2370

EDIT

Actually it's easier: the round function supports these options:

>> 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

这篇关于四舍五入为有效数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆