消息内的数字格式很好

发布于 2024-10-20 17:30:46 字数 465 浏览 0 评论 0原文

默认情况下,使用 StyleBox 打印字符串时,我们会在字符串内得到格式良好的数字:

StyleBox["some text 1000000"] // DisplayForm

我的意思是,这些数字看起来好像会有额外的小空格:“1 000 000”。

但在 Message 中,所有数字均显示为无格式:

f::NoMoreMemory = 
  "There are less than `1` bytes of free physical memory (`2` bytes \
is free). $Failed is returned.";
Message[f::NoMoreMemory, 1000000, 98000000]

有没有办法让 Message 中的数字进行格式化?

When printing string with StyleBox by default we get nicely formatted numbers inside string:

StyleBox["some text 1000000"] // DisplayForm

I mean that the numbers look as if would have additional little spaces: "1 000 000".

But in Messages all numbers are displayed without formatting:

f::NoMoreMemory = 
  "There are less than `1` bytes of free physical memory (`2` bytes \
is free). $Failed is returned.";
Message[f::NoMoreMemory, 1000000, 98000000]

Is there a way to get numbers inside Messages to be formatted?

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

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

发布评论

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

评论(2

寂寞清仓 2024-10-27 17:30:46

我将使用 Style 来应用 AutoNumberFormatting 选项:

您可以使用它来定位特定消息:

f::NoMoreMemory = 
 "There are less than `1` bytes of free physical memory (`2` bytes is free). $Failed is returned.";

Message[f::NoMoreMemory, 
 Style[1000000, AutoNumberFormatting -> True], 
 Style[98000000, AutoNumberFormatting -> True]]

或者您可以将它与 $MessagePrePrint 一起使用以将其应用到所有消息:

$MessagePrePrint = Style[#, AutoNumberFormatting -> True] &;

Message[f::NoMoreMemory, 1000000, 98000000]

I'd use Style to apply the AutoNumberFormatting option:

You can use it to target specific messages:

f::NoMoreMemory = 
 "There are less than `1` bytes of free physical memory (`2` bytes is free). $Failed is returned.";

Message[f::NoMoreMemory, 
 Style[1000000, AutoNumberFormatting -> True], 
 Style[98000000, AutoNumberFormatting -> True]]

or you can use it with $MessagePrePrint to apply it to all the messages:

$MessagePrePrint = Style[#, AutoNumberFormatting -> True] &;

Message[f::NoMoreMemory, 1000000, 98000000]
娇纵 2024-10-27 17:30:46

我认为你想要 $MessagePrePrint

$MessagePrePrint = 
   NumberForm[#, DigitBlock -> 3, NumberSeparator -> " "] &;

或者,结合 Sjoerd 的建议:

With[
  {opts =
    AbsoluteOptions[EvaluationNotebook[],
     {DigitBlock, NumberSeparator}]},
  $MessagePrePrint = NumberForm[#, Sequence @@ opts] &];

采用 Brett Champion 的方法,我相信这允许复制和复制。按您的要求粘贴:

$MessagePrePrint = StyleForm[#, AutoNumberFormatting -> True] &;

I think you want $MessagePrePrint

$MessagePrePrint = 
   NumberForm[#, DigitBlock -> 3, NumberSeparator -> " "] &;

Or, incorporating Sjoerd's suggestion:

With[
  {opts =
    AbsoluteOptions[EvaluationNotebook[],
     {DigitBlock, NumberSeparator}]},
  $MessagePrePrint = NumberForm[#, Sequence @@ opts] &];

Adapting Brett Champion's method, I believe this allows for copy & paste as you requested:

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