Hi Bradley,
That deals with accepting answers with commas but you should be able to reverse the process. I do not recall that Davide Cervone has an option to output numbers with commas using MathObjects which would be the easiest solution.
Arnie
Try using regular expressions. I think the following should work, if you create a string from your number using standard formatting. In my first attempt, I did not realize that .pg files have to use a different escape character even in regular expressions.
Here is some sample code that works for the numbers I've tried. Essentially, there is a loop that replaces strings of 4 digits with a comma before the last 3, working from the decimal place to the left. However, it also needs to take into account the possibility of no decimals and needs to avoid putting commas to the right of the decimal place.
$x = 123457890;
$xStr = sprintf("%d", $x);
while ($xStr =~ s/(^[+-]?~~d*)(~~d)(~~d{3})((,~~d{3}|~~.~~d*)*)$/~~1~~2,~~3~~4/) {}
$y = 123456789.123456;
$yStr = sprintf("%f", $y);
while ($yStr =~ s/(^[+-]?~~d*)(~~d)(~~d{3})((,~~d{3}|~~.~~d*)*)$/~~1~~2,~~3~~4/) {}
$z = -123457890.987654321;
$zStr = sprintf("%f", $z);
while ($zStr =~ s/(^[+-]?~~d*)(~~d)(~~d{3})((,~~d{3}|~~.~~d*)*)$/~~1~~2,~~3~~4/) {}
Good luck,
- Brian
D. Brian Walton
James Madison University
Here is some sample code that works for the numbers I've tried. Essentially, there is a loop that replaces strings of 4 digits with a comma before the last 3, working from the decimal place to the left. However, it also needs to take into account the possibility of no decimals and needs to avoid putting commas to the right of the decimal place.
$x = 123457890;
$xStr = sprintf("%d", $x);
while ($xStr =~ s/(^[+-]?~~d*)(~~d)(~~d{3})((,~~d{3}|~~.~~d*)*)$/~~1~~2,~~3~~4/) {}
$y = 123456789.123456;
$yStr = sprintf("%f", $y);
while ($yStr =~ s/(^[+-]?~~d*)(~~d)(~~d{3})((,~~d{3}|~~.~~d*)*)$/~~1~~2,~~3~~4/) {}
$z = -123457890.987654321;
$zStr = sprintf("%f", $z);
while ($zStr =~ s/(^[+-]?~~d*)(~~d)(~~d{3})((,~~d{3}|~~.~~d*)*)$/~~1~~2,~~3~~4/) {}
Good luck,
- Brian
D. Brian Walton
James Madison University
Perhaps this discussion could morph into a wiki-page which describes cosmetics of numbers, numerals, and scientific notation (which is a far-cry from numerology ;-)).
For instance, I might like to display a dollars-and-cents value as $12.70 rather than $12.7 but can accept $12.7 for now while I focus on higher-level stuff to present to my students. (I do not want "BEST" to be enemy of "GOOD".)
PS: thanks Arnie and Brian for reference and code-fragments.