显示具有指定有效位数的数字

发布于 2024-10-20 15:47:05 字数 991 浏览 4 评论 0原文

我使用以下函数将数字转换为字符串以用于显示目的(不要使用科学记数法,不要使用尾随点,按指定舍入):(

(* 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 技术交流群。

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

发布评论

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

评论(3

冰雪梦之恋 2024-10-27 15:47:05

Dreeves,我想我终于明白你想要什么了,而且你已经拥有了,差不多了。如果没有,请再次尝试解释我所缺少的内容。

shn2[x_, r_: 0] := 
 StringReplace[
  ToString@NumberForm[x, r, ExponentFunction -> (Null &)], 
  RegularExpression@"\\.0*$" -> ""]

测试:

shn2[#, 4] & /@ {123.456, 0.00123456}
shn2[#, {3, 1}] & /@ {789.0, 0.789}
shn2[#, {10, 2}] & /@ {0.1234, 1234.}
shn2[#, {4, 1}] & /@ {12.34, 1234.56}

Out[1]= {"123.5", "0.001235"}

Out[2]= {"789", "0.8"}

Out[3]= {"0.12", "1234"}

Out[4]= {"12.3", "1235"}

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.

shn2[x_, r_: 0] := 
 StringReplace[
  ToString@NumberForm[x, r, ExponentFunction -> (Null &)], 
  RegularExpression@"\\.0*$" -> ""]

Testing:

shn2[#, 4] & /@ {123.456, 0.00123456}
shn2[#, {3, 1}] & /@ {789.0, 0.789}
shn2[#, {10, 2}] & /@ {0.1234, 1234.}
shn2[#, {4, 1}] & /@ {12.34, 1234.56}

Out[1]= {"123.5", "0.001235"}

Out[2]= {"789", "0.8"}

Out[3]= {"0.12", "1234"}

Out[4]= {"12.3", "1235"}
極樂鬼 2024-10-27 15:47:05

这可能不是完整的答案(您需要从字符串转换为字符串),但此函数接受参数为数字 x 和有效数字 sig 。它保留的位数是sig的最大值或小数点左边的位数。

A[x_,sig_]:=NumberForm[x, Max[Last[RealDigits[x]], 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 figures sig wanted. The number of digits it keeps is the maximum of sig or the number of digits to the left of the decimal.

A[x_,sig_]:=NumberForm[x, Max[Last[RealDigits[x]], sig]]

RealDigits

写给空气的情书 2024-10-27 15:47:05

这是我的原始函数的可能概括。
(我已经确定它与 Wizard 先生的解决方案不同,但我还不确定我认为哪个更好。)

re = RegularExpression;

(* Show Number. Convert to string w/ no trailing dot. Use at most d significant
   figures after the decimal point. Target t significant figures total (clipped 
   to be at least i and at most i+d, where i is the number of digits in integer 
   part of x). *)
shn[x_, d_:5, t_:16] := ToString[x]
shn[x_?NumericQ, d_:5, t_:16] := With[{i= IntegerLength@IntegerPart@x},
  StringReplace[ToString@NumberForm[N@x, Clip[t, {i,i+d}],
                                    ExponentFunction->(Null&)],
                re@"\\.$"->""]]

测试:

这里我们指定 4 位有效数字,但永远不会将任何数字放在小数点左侧,并且永远不会使用小数点右侧多于 2 位有效数字。

(# -> shn[#, 2, 4])& /@ 
  {123456, 1234.4567, 123.456, 12.345, 1.234, 1.0001, 0.123, .0001234}

{  123456 -> "123456", 
 1234.456 -> "1234", 
  123.456 -> "123.5"
   12.345 -> "12.35", 
    1.234 -> "1.23", 
   1.0001 -> "1", 
    0.123 -> "0.12", 
0.0001234 -> "0.00012" }

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

re = RegularExpression;

(* Show Number. Convert to string w/ no trailing dot. Use at most d significant
   figures after the decimal point. Target t significant figures total (clipped 
   to be at least i and at most i+d, where i is the number of digits in integer 
   part of x). *)
shn[x_, d_:5, t_:16] := ToString[x]
shn[x_?NumericQ, d_:5, t_:16] := With[{i= IntegerLength@IntegerPart@x},
  StringReplace[ToString@NumberForm[N@x, Clip[t, {i,i+d}],
                                    ExponentFunction->(Null&)],
                re@"\\.$"->""]]

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.

(# -> shn[#, 2, 4])& /@ 
  {123456, 1234.4567, 123.456, 12.345, 1.234, 1.0001, 0.123, .0001234}

{  123456 -> "123456", 
 1234.456 -> "1234", 
  123.456 -> "123.5"
   12.345 -> "12.35", 
    1.234 -> "1.23", 
   1.0001 -> "1", 
    0.123 -> "0.12", 
0.0001234 -> "0.00012" }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文