WeBWorK Problems

currency amounts with B, M, K

currency amounts with B, M, K

by Alex Jordan -
Number of replies: 0

I have an exercise where the answers are very large currency amounts, in the millions or billions of dollars. I am using the Currency context for this exercise. I thought it would be nice to let students type something like $432M or $0.432B instead of $432000000. So I added some constants to the context:

Context()->constants->add(
    B => 10**9,
    M => 10**6,
    K => 10**3
);

That is enough to let me create currency objects in this way:
$ans = Currency("$432 M");

However it's not enough to let students enter "$432 M" or "$0.432B". If they submit "$432M", there is an error:
Can't convert a real number to a monetary value
I have only been able to get past this by passing promoteReals => 1 to the cmp() routine. And this is progress, because then "$432M" is accepted.

I still can't submit "$0.432B" or I get:
Monetary values must have exactly two decimal places
But I can address that with the flag:
noExtraDecimals => 0

This is almost what I want. Except that since I am using promoteReals => 1, the students can enter answers without a dollar sign. I'd prefer to still require the currency symbol and give the usual message about the answer not being a currency when the currency symbol is omitted.

Big picture: is there a better way to get things like "B", "M", and "K" to work as I'd like them to in the currency context?

Focusing on what I've tried: can my approach be adjusted to require the dollar sign, but get past the "Can't convert a real number to a monetary value" issue?