显示具有指定有效位数的数字
我使用以下函数将数字转换为字符串以用于显示目的(不要使用科学记数法,不要使用尾随点,按指定舍入):(
(* Show Number. Convert to string w/ no trailing dot. Round to the nearest r. *)
Unprotect[Round]; Round[x_,0] := x; Protect[Round];
shn[x_, r_:0] := StringReplace[
ToString@NumberForm[Round[N@x,r], ExponentFunction->(Null&)], re@"\\.$"->""]
请注意,re
是一个别名对于RegularExpression
。)
这多年来一直对我很有帮助。 但有时我不想指定要舍入的位数,而是想指定有效数字的数量。 例如,123.456 应显示为 123.5,但 0.00123456 应显示为 0.001235。
为了变得真正奇特,我可能想在小数点之前和之后指定有效数字。 例如,我可能希望 0.789 显示为 0.8,但 789.0 显示为 789 而不是 800。
您是否有一个方便的实用函数来处理此类事情,或者有关于概括我上面的函数的建议吗?
相关:抑制尾随“.”在 Mathematica 的数字输出中
更新: 我尝试在这里询问这个问题的一般版本:
https://stackoverflow.com/questions/5627185/displaying-numbers-to-non-技术用户
I use the following function to convert a number to a string for display purposes (don't use scientific notation, don't use a trailing dot, round as specified):
(* Show Number. Convert to string w/ no trailing dot. Round to the nearest r. *)
Unprotect[Round]; Round[x_,0] := x; Protect[Round];
shn[x_, r_:0] := StringReplace[
ToString@NumberForm[Round[N@x,r], ExponentFunction->(Null&)], re@"\\.$"->""]
(Note that re
is an alias for RegularExpression
.)
That's been serving me well for years.
But sometimes I don't want to specify the number of digits to round to, rather I want to specify a number of significant figures.
For example, 123.456 should display as 123.5 but 0.00123456 should display as 0.001235.
To get really fancy, I might want to specify significant digits both before and after the decimal point.
For example, I might want .789 to display as 0.8 but 789.0 to display as 789 rather than 800.
Do you have a handy utility function for this sort of thing, or suggestions for generalizing my function above?
Related: Suppressing a trailing "." in numerical output from Mathematica
UPDATE: I tried asking a general version of this question here:
https://stackoverflow.com/questions/5627185/displaying-numbers-to-non-technical-users
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Dreeves,我想我终于明白你想要什么了,而且你已经拥有了,差不多了。如果没有,请再次尝试解释我所缺少的内容。
测试:
dreeves, I think I finally understand what you want, and you already had it, pretty much. If not, please try again to explain what I am missing.
Testing:
这可能不是完整的答案(您需要从字符串转换为字符串),但此函数接受参数为数字
x
和有效数字sig
。它保留的位数是sig
的最大值或小数点左边的位数。RealDigits
This may not be the complete answer (you need to convert from/to string), but this function takes arguments a number
x
and significant figuressig
wanted. The number of digits it keeps is the maximum ofsig
or the number of digits to the left of the decimal.RealDigits
这是我的原始函数的可能概括。
(我已经确定它与 Wizard 先生的解决方案不同,但我还不确定我认为哪个更好。)
测试:
这里我们指定 4 位有效数字,但永远不会将任何数字放在小数点左侧,并且永远不会使用小数点右侧多于 2 位有效数字。
Here's a possible generalization of my original function.
(I've determined that it's not equivalent to Mr Wizard's solution but I'm not sure yet which I think is better.)
Testing:
Here we specify 4 significant digits, but never dropping any to the left of the decimal point and never using more than 2 significant digits to the right of the decimal point.