WeBWorK Main Forum

Formatting numbers -- using commas as thousands-separator

Formatting numbers -- using commas as thousands-separator

by Gabor Lukacs -
Number of replies: 11

Hi,

I was wondering if there is a reasonably easy way to format currencies as $1,234,567.00.

In perl, I the Number::Format library, but it is not designated as "safe," and so I cannot use it in the problems that I am drafting. 

Is there a workaround for this?

Thank you in advance,
Gabor

In reply to Gabor Lukacs

Re: Formatting numbers -- using commas as thousands-separator

by Alex Jordan -

You could use the Currency context:

https://github.com/openwebwork/pg/blob/master/macros/contextCurrency.pl

So make:
$c = Currency(1234567);
and $c will display as you asked. You can also customize things with the context flags. Like whether you automatically drop a ".00" at the end. Review the documentation at the top to see all the options.

In reply to Alex Jordan

Re: Formatting numbers -- using commas as thousands-separator

by Michael Gage -

And here is the POD (plain old documentation) description from the code. 

https://demo.webwork.rochester.edu/wwdocs/pg/macros/contextCurrency.pl.html


I don't think anyone has gotten around to writing a tutorial for this in Techniques part of the wiki.  (Access to PODs are from the main Author page.)


In reply to Michael Gage

Re: Formatting numbers -- using commas as thousands-separator

by Alex Jordan -
The POD links on certain pages are still not updated from the move. I tried to navigate to this one by getting here first:
https://webwork.maa.org/wiki/Specialized_contexts
but the link to Currency is broken. I can't recall if there is a way to globally update these, or if it just comes down to tracking them all down and manually editing.
In reply to Alex Jordan

Re: Formatting numbers -- using commas as thousands-separator

by Glenn Rice -
Yeah. The documentation is hosted at https://demo.webwork.rochester.edu, and needs to be migrated to the webwork.maa.org server I think. For now, start by going to https://webwork.maa.org/wiki/Authors and clicking on the POD link there, and those take you to the Rochester server.
In reply to Alex Jordan

Re: Formatting numbers -- using commas as thousands-separator

by Gabor Lukacs -
Thank you, Alex. This is exactly what I was looking for.

I wonder, is there a way to change contexts mid-question? If I need one context in part (a) of the question, and a different one (say Numeric / expressions with real variables) in part (b)?
In reply to Gabor Lukacs

Re: Formatting numbers -- using commas as thousands-separator

by Alex Jordan -

The short answer is, yes.

A Context is a hash containing information about how MathObjects should be handled. When you create a MathObject (a Formula, a Real, a Currency, etc), that MO itself is a hash that has a reference to a Context (usually the Context that was active at the time the MO was created). For one thing, you can modify that Context mid-problem. Just understand that the MO may be handled differently depending on where in the problem code these things are happening.

Then you can also make new Contexts, changing the active Context. Contexts that were created earlier still exist, and their MathObjects still point to them. 

You can have multiple Contexts that are "the same". Meaning, they are different hashes, but have all the same properties.

Example:

Context("Numeric");  # Context "A"
Context()->flags->set(reduceConstants=>0);  # modifies Context "A"
$f = Formula("1/2 x^2");  # f is in Context "A"

Context("Numeric");  # Context "B"
$g = Formula("1/2 x^2");  # g is in Context B

$r = $f->eval(x=>2);  # $r is a Real in Context "A"
$s = $g->eval(x=>2);  # $r is a Real in Context "B"
$r = Real($r);  # now $r is a Real in Context "B"

# Printing $f right now in math mode will make `\frac{1}{2} x^2`, because it is in Context "A"
# Printing $g right now in math mode will make `0.5 x^2`, because it is in Context "B"
# Similarly, any context settings that affect answer checking could affect assessing student answers for $f and $g

In reply to Alex Jordan

Re: Formatting numbers -- using commas as thousands-separator

by Gabor Lukacs -
Thank you so much, Alex, for the detailed answer.

Is there a context for equations, that is, a context that can treat expressions like "2*x-y=2" and is capable of identifying it is with "-2*x+y=-2" and "2*x -y-2=0"?
In reply to Gabor Lukacs

Re: Formatting numbers -- using commas as thousands-separator

by Alex Jordan -

Yes, there is this:

https://webwork.maa.org/wiki/EquationEvaluators

Some care/awareness is needed to use this. It works by choosing random (x,y) values (or however many variables are in the context) and using a kind of Newton's method to converge toward tuples that solve the equation. And it sees if the same convergence happens for the student answer. There are kinds of equations where that just won't work. See the details in the commentary at:

https://github.com/openwebwork/pg/blob/master/macros/parserImplicitEquation.pl


In reply to Alex Jordan

Re: Formatting numbers -- using commas as thousands-separator

by Gabor Lukacs -
Thank you Alex for suggestion the EquationEvaluators / ImplicitEquation. It one seems to be checking equality on the basis of testing values, though.

I was looking at for something that is more based on formal algebra. A step forward from reducing algebraic expressions.
In reply to Gabor Lukacs

Re: Formatting numbers -- using commas as thousands-separator

by Alex Jordan -

Hi Gabor,

Since WeBWorK does not have a CAS, it doesn't have what I think you are describing.

You can have a WeBWorK problem appeal to Sage for certain things. Try leveraging what is here:

https://webwork.maa.org/wiki/AskSage

It has been a long time since I tried to use this. IIRC, occasionally you will have to be careful with the syntax that PG understands versus the syntax that Sage understands. And it sounds like you may be interested in using AskSage within a custom answer checker, not just using it to produce something to present in the exercise statement.

Alex