按照我想要的格式设置数字

发布于 2024-12-13 14:10:31 字数 263 浏览 4 评论 0原文

我有号码:a = 3.860575156847749e+003;,我会以正常方式显示它。所以我写b = sprintf('%0.1f' a);。如果我打印 b 我将得到:3860.6。这是完美的。事实上,虽然a是double类型,但b已经转换为char。

我可以做什么来正确格式化该数字并仍然得到一个数字作为最终结果?

此致

I have the number: a = 3.860575156847749e+003; and I would show it in a normal manner. So I write b = sprintf('%0.1f' a);. If I print b I will get: 3860.6. This is perfect. Matter of fact, while a is a double type, b has been converted in char.

What can I do to proper format that number and still have a number as final result?

Best regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-12-20 14:10:31

那么,您必须区分数值(存储在计算机内存中的数字)及其十进制表示形式(您在屏幕上看到的字符串/字符数组)。您不能真正对数字强加格式:数字具有可以以不同方式表示为字符串的值(例如 1234 = 1.234e3 = 12.34e2 = 0.1234e4 = ... )。

如果要存储精度较低的数字,可以使用roundfloorceil来计算精度低于原始号码。
例如,如果您有 a = 3.860575156847749e+003 并且您想要一个只有 5 位有效数字的数字,则可以使用 round 来实现:

a = 3.860575156847749e+003;
p = 0.1; % absolute precision you want
b = p .* round(a./p)

这将产生一个变量 < code>b = 3.8606e3 可以用不同的方式表示,但在第五个之后应该包含零(实际上:非常小的值有时是不可避免的)数字。我认为这就是您真正想要的,但请记住,对于计算机来说,这个数字也等于 3.86060000 (它只是同一值的另一个字符串表示形式),所以我想再次强调十进制表示形式不是通过对数字进行四舍五入来设置的,而是通过(隐式)调用将双精度数转换为字符串的函数来设置,这可以通过 sprintfdisp 或可能的某些函数来设置其他功能。

Well, you have to distinguish between both the numerical value (the number stored in your computer's memory) and its decimal representation (the string/char array you see on your screen). You can't really impose a format on a number: a number has a value which can be represented as a string in different ways (e.g. 1234 = 1.234e3 = 12.34e2 = 0.1234e4 = ...).

If you want to store a number with less precision, you can use round, floor, ceil to calculate a number which has less precision than the original number.
E.g. if you have a = 3.860575156847749e+003 and you want a number that only has 5 significant digits, you can do so by using round:

a = 3.860575156847749e+003;
p = 0.1; % absolute precision you want
b = p .* round(a./p)

This will yield a variable b = 3.8606e3 which can be represented in different ways, but should contain zeros (in practice: very small values are sometimes unavoidable) after the fifth digit. I think that is what you actually want, but remember that for a computer this number is equal to 3.86060000 as well (it is just another string representation of the same value), so I want to stress again that the decimal representation is not set by rounding the number but by (implicitly) calling a function that converts the double to a string, which happens either by sprintf, disp or possibly some other functions.

鹊巢 2024-12-20 14:10:31

sprintf ya 文本变量的结果。您是否尝试过将变量声明为整数(例如)并将其用作 sprintf 指令的返回值?

这对您可能有用:http://blogs .mathworks.com/loren/2006/12/27/displaying-numbers-in-matlab/

Result of sprintf y a text variable. have you tried to declare a variable as integer (for example) and use this as return value for sprintf instruction?

This can be useful to you: http://blogs.mathworks.com/loren/2006/12/27/displaying-numbers-in-matlab/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文