Difference between revisions of "FormattingDecimals"
m |
m |
||
Line 39: | Line 39: | ||
<td style="background-color:#ffffdd;border:black 1px dashed;"> |
<td style="background-color:#ffffdd;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | $ |
+ | $a = random(3,7,1); |
# log is natural log, and ln is also natural log |
# log is natural log, and ln is also natural log |
||
− | $ |
+ | $b = sprintf("%0.3f", log($a)/log(10) ); |
</pre> |
</pre> |
||
</td> |
</td> |
||
Line 48: | Line 48: | ||
<b>Setup:</b> |
<b>Setup:</b> |
||
Use perl's <code>sprintf( format, number );</code> command to format the decimal. The <code>"%0.3f"</code> portion truncates after 3 decimal places and uses zeros (not spaces) to right-justify. For answers involving money, you should set <code>"%0.2f"</code> for two decimal places and zero filling (for example, <code>sprintf("%0.2f",0.5);</code> returns <code>0.50</code>). You can do a web search for more options to perl's <code>sprintf</code>, and also for WeBWorK's <code>contextCurrency.pl</code>. If you do further calculations with <code>$a</code>, be aware that numerical error may be an issue since you've reduced the number of decimal places. |
Use perl's <code>sprintf( format, number );</code> command to format the decimal. The <code>"%0.3f"</code> portion truncates after 3 decimal places and uses zeros (not spaces) to right-justify. For answers involving money, you should set <code>"%0.2f"</code> for two decimal places and zero filling (for example, <code>sprintf("%0.2f",0.5);</code> returns <code>0.50</code>). You can do a web search for more options to perl's <code>sprintf</code>, and also for WeBWorK's <code>contextCurrency.pl</code>. If you do further calculations with <code>$a</code>, be aware that numerical error may be an issue since you've reduced the number of decimal places. |
||
+ | </p> |
||
+ | <p> |
||
+ | We used the logarithm change of base formula log<sub>10</sub>(a) = log(a) / log(10) = ln(a) / ln(10) to get a logarithm base 10. |
||
</p> |
</p> |
||
<p> |
<p> |
||
Line 62: | Line 65: | ||
BEGIN_TEXT |
BEGIN_TEXT |
||
− | \( $ |
+ | \( $b = \) \{ ans_rule(20) \} |
END_TEXT |
END_TEXT |
||
Line 79: | Line 82: | ||
<td style="background-color:#eeddff;border:black 1px dashed;"> |
<td style="background-color:#eeddff;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | ANS( $ |
+ | ANS( $b->cmp() ); |
</pre> |
</pre> |
||
<td style="background-color:#eeccff;padding:7px;"> |
<td style="background-color:#eeccff;padding:7px;"> |
Revision as of 14:16, 16 January 2010
Formatting Decimals: PG Code Snippet
We show how to format decimals for display in PG problems. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.
PG problem file | Explanation |
---|---|
loadMacros("PGstandard.pl","MathObjects.pl"); |
Initialization: Standard. |
$a = random(3,7,1); # log is natural log, and ln is also natural log $b = sprintf("%0.3f", log($a)/log(10) ); |
Setup:
Use perl's We used the logarithm change of base formula log10(a) = log(a) / log(10) = ln(a) / ln(10) to get a logarithm base 10.
Note: If we load |
BEGIN_TEXT \( $b = \) \{ ans_rule(20) \} END_TEXT |
Main Text: Display the formatted number. |
ANS( $b->cmp() ); |
Answer Evaluation: Standard. |