Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nice formatting of numbers inside Messages

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?

like image 780
Alexey Popkov Avatar asked Mar 08 '11 11:03

Alexey Popkov


2 Answers

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]
like image 101
Brett Champion Avatar answered Sep 16 '22 20:09

Brett Champion


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] &;
like image 43
Mr.Wizard Avatar answered Sep 20 '22 20:09

Mr.Wizard