如何显示一定数量的小数位数
在Matlab中,如何控制命令窗口中显示的小数位数?
例如,
>> x=0.4654
x=
<前><代码>0.4654
如何在命令窗口中将变量x的值分别显示为0.5、0.47、0.465?
谢谢!
In Matlab, how to control the number of decimal digits for displaying in command window?
For example,
>> x=0.4654
x =
0.4654
how to display the value of the variable x as 0.5, 0.47, 0.465 respectively in command window?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为没有内置舍入到任意位置,但您可以通过执行 round(x*10^位置数)/10^位置数<来获得舍入结果/em>.这会打印出尾随的零,如果你想去掉那些你必须做一个特殊格式的打印,比如 sprintf 到度数,所以在你的情况下,你可以通过这样做得到你想要的结果:
我希望这有帮助!
编辑:如果你想对矩阵执行此操作,我不确定是否有更好的方法,但你可以循环遍历给定 x 作为矩阵的行:
I don't think there is built in rounding to arbitrary places, but you can achieve the rounded result by doing round(x*10^number of places)/10^number of places. This prints out with the trailing zeroes, if you want to get rid of those you have to do a specially formatted print like sprintf to the degrees so in your case you could get the results you want by doing:
I hope that helps!
Edit: If you want to do it for matrices, I'm not sure if there's a better way but you could just loop over the rows given x as a matrix:
您可以使用
format
命令控制命令窗口。我建议您查看doc format
,它为您提供了特定的选项。如果您需要更高的精度,最好使用
disp
和/或舍入函数,例如:z = round(x/y)*y
You have control over the command window using the
format
command. I suggest you take a look atdoc format
, which gives you specific options.If you need more precision, you'd be better off using
disp
, and / or a rounding function such as:z = round(x/y)*y
更改格式。
Change the format.