## WeBWorK Main Forum

### commas in the question

Number of replies: 3
Is there a method to getting a large number in the problem itself to have commas?  I randomly generate about 5 numbers for one problem that are numbers like 10,000,000 or around that number, but I am unsure of how exactly to get those commas in a randomly generated number.

### Re: commas in the question

by Arnold Pizer -

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

### Re: commas in the question

by D. Brian Walton -
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 In reply to D. Brian Walton ### Re: commas in the question by Dick Lane - 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.