如何在Matlab中将数字四舍五入到10个正确的小数位 [英] How to round a number to 10 correct decimal places in Matlab

查看:185
本文介绍了如何在Matlab中将数字四舍五入到10个正确的小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将x和g(x)的值四舍五入到小数点后10位?我觉得fprintf('%0.10', number)没有提供所需的输出,是吗???我的输出是给我数字最多6个小数位而不是10:

How can I round values of x and g(x) to 10 decimal places?? I feel like fprintf('%0.10', number) does not give the desired output, does it???? My output is giving me the numbers up to 6 decimal places instead of 10:

 fpi1(g, 0.5, 60)
0.500000            1.951392 
1.951392            1.706095 
1.706095            1.543600 
1.543600            1.462184 
1.462184            1.430210 
1.430210            1.419396 
1.419396            1.415971 
1.415971            1.414911 
1.414911            1.414585 
1.414585            1.414485 
1.414485            1.414454 
1.414454            1.414445 
1.414445            1.414442 
1.414442            1.414441 
1.414441            1.414441 
1.414441            1.414441 
1.414441            1.414441 
1.414441            1.414441 
1.414441            1.414441 
1.414441            1.414441 
1.414441            1.414441 
1.414441            1.414441 
1.414441            1.414441 

要解决的命令是什么,即使输出到小数点后10位?预先感谢!

What is the command to fix it, i.e. to make the output to 10 decimal places? Thanks in advance!

%Program 1.2 Fixed-Point Iteration
%Computes approximate solution of g(x)=x
%Input: inline function g, starting guess x0, 
%       number of steps k
%Output: Approximate solution xc
function xc=fpi1(g,x0,k)
format long;
x(1)=x0;
for i=1:k
  x(i+1)=g(x(i));

  fprintf('%f            %f \n', x(i), g(x(i)));
  %printf(x(i), g(x(i)));
end
xc=x(k+1);

推荐答案

它是fprintf('%0.10f', number);.您在说明符中忘记了f.

It's fprintf('%0.10f', number);. You forgot the f in the specifier.

>> number = 141421.1240981320948710948018941;
>> fprintf('%0.10f\n', number)
141421.1240981321

这篇关于如何在Matlab中将数字四舍五入到10个正确的小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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