Difference between revisions of "FormattingDecimals"

From WeBWorK_wiki
Jump to navigation Jump to search
m
m
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>
  +
Note: If we load <code>MathObjects.pl</code>, then both <code>log</code> and <code>ln</code> are defined to be the natural logarithm (base e, not base 10). If we had loaded the older <code>PGauxiliaryFunctions.pl</code> macro instead, then <code>ln</code> would be undefined and <code>log</code> would be defined as the natural logarithm (base e, not base 10).
 
</p>
 
</p>
 
</td>
 
</td>

Revision as of 14:06, 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.

Problem Techniques Index

PG problem file Explanation
loadMacros("PGstandard.pl","MathObjects.pl");

Initialization: Standard.

$b = random(3,7,1);
# log is natural log, and ln is also natural log
$a = sprintf("%0.3f", log($b)/log(10) );

Setup: Use perl's sprintf( format, number ); command to format the decimal. The "%0.3f" portion truncates after 3 decimal places and uses zeros (not spaces) to right-justify. For answers involving money, you should set "%0.2f" for two decimal places and zero filling (for example, sprintf("%0.2f",0.5); returns 0.50). You can do a web search for more options to perl's sprintf, and also for WeBWorK's contextCurrency.pl. If you do further calculations with $a, be aware that numerical error may be an issue since you've reduced the number of decimal places.

Note: If we load MathObjects.pl, then both log and ln are defined to be the natural logarithm (base e, not base 10). If we had loaded the older PGauxiliaryFunctions.pl macro instead, then ln would be undefined and log would be defined as the natural logarithm (base e, not base 10).

BEGIN_TEXT

\( $a = \) \{ ans_rule(20) \}

END_TEXT

Main Text: Display the formatted number.

ANS( $a->cmp() );

Answer Evaluation: Standard.

Problem Techniques Index