## WeBWorK Problems

### answer checker for finance math problems to accept and display 2 decimal digits

by Aba Mbirika -
Number of replies: 3
Hello,

I am looking in the NPL and getting many varying codes for the answer-checker for finance math problems. MY GOAL: for students to enter answers like 121.22 (or even $121.22) when the numerical answer to a finance math problem is 121.2161241 for example. When I look through the NPL examples, I see three types of code: 1. ANS(num_cmp($ans, format => '%0.2f', tol => .01));
This does what I need, but I am not sure what the %0.2f means. Also just after the preamble following the TEXT(beginproblem());, I see:
Context("Numeric");
Context()->flags->set( tolerance=>0, tolType=>'absolute' );

I am wondering if the latter is needed (i.e., does the tol=>.01 in the former override the tolerance=>0 in the latter?).

2. ANS( Compute($ans)->cmp() ); This does NOT do what I need, because in the answer checker it displays answers like 121.2161241 as the correct answer, and I prefer the answer to have only 2 decimal digits of accuracy since the answer is a dollar/cents amount. 3. Unlike the two examples, I have seen code that has "contextCurrency.pl" in the preamble for the macros, and then code like: Context("Currency"); Context()->flags->set(trimTrailingZeros=>1);$ans = Currency("$an"); And that is followed by the following in the answer checker by: ANS($ans->cmp(promoteReals=>1,tolerance=>0.01));

So I am a little (perhaps 'a lot') confused here by the varying examples in the NPL. Any advice is very welcome.

aBa

### Re: answer checker for finance math problems to accept and display 2 decimal digits

by Steve Dalton -
'%0.2f' means you are formatting the answer to have 2 digits after the decimal point. For example, '%0.4f' would give 4 digits after the decimal point.  It is an example of a printf placeholder.  To dissect it completely, the '0' means to prepend a 0 if none is present, the '.2' says to use 2-digit precision, and the 'f' specifies the type is floating-point.

Tolerance for a particular answer-checker overrides the global context setting.

I would suggest that if your answers will be in dollars (or some other currency), use the Currency context.  If you just want to specify a number of digits after the decimal point, use option #1.

### Re: answer checker for finance math problems to accept and display 2 decimal digits

by Hedley Pinsent -

When I taught business math (in a trades/technology setting) it was a scramble and I never got around to such refinements.

However it seems if you set the answer (at the start) to the rounded or truncated value you would avoid the trailing decimals.

$answer = int(100*$answer)/100 ; # to round

$answer = int(100*$answer -0.5)/100 ; # to truncate a value

On another topic -

Attached are the problems I used; some of the multiple choice/match questions may be proprietary to the textbook? The rest seems generic. They also push the use of the HP- 10bII financial calculator - oh and Canadian examples.

The quick look can be found at http://www.teabun.ca/webwork2/MA2400_sample

The attachment extracts to Assignment01, Assignment02, ... Assignment10

hp

### Re: answer checker for finance math problems to accept and display 2 decimal digits

by Hedley Pinsent -
ooops - sorry

The above is wrong - int will generally (?) truncate.

Perhaps it would be best to use the "round" function loaded with PGstandard

Years ago Fortran IV (with WATFIV) we rounded with int(x+0.5)
Then a new version was installed and the rounding was done automatically.
I thought this to be a very progressive at the time; I guess perl has regressed.

many regards
hp