Skip to content

Commit

Permalink
update prettysigfig with choptrailingzeros flag
Browse files Browse the repository at this point in the history
git-svn-id: http://imathas.googlecode.com/svn/trunk@710 c89b4f0b-ac2a-0410-9773-c9071ee4f95d
  • Loading branch information
drlippman committed May 4, 2011
1 parent 4213be1 commit 9375e4f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
16 changes: 16 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
AddType image/svg+xml .svg
AddType image/svg+xml .svgz
AddType image/x-icon .ico
AddType application/vnd.ms-word.document.macroEnabled.12 .docm
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.document docx
AddType application/vnd.openxmlformats-officedocument.wordprocessingml.template dotx
AddType application/vnd.ms-powerpoint.template.macroEnabled.12 potm
AddType application/vnd.openxmlformats-officedocument.presentationml.template potx
AddType application/vnd.ms-powerpoint.addin.macroEnabled.12 ppam
AddType application/vnd.ms-powerpoint.slideshow.macroEnabled.12 ppsm
AddType application/vnd.openxmlformats-officedocument.presentationml.slideshow ppsx
AddType application/vnd.ms-powerpoint.presentation.macroEnabled.12 pptm
AddType application/vnd.openxmlformats-officedocument.presentationml.presentation pptx
AddType application/vnd.ms-excel.addin.macroEnabled.12 xlam
AddType application/vnd.ms-excel.sheet.binary.macroEnabled.12 xlsb
AddType application/vnd.ms-excel.sheet.macroEnabled.12 xlsm
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
AddType application/vnd.ms-excel.template.macroEnabled.12 xltm
AddType application/vnd.openxmlformats-officedocument.spreadsheetml.template xltx
Options -Indexes

AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml application/x-javascript text/javascript application/javascript
Expand Down
25 changes: 18 additions & 7 deletions assessment/macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -1402,19 +1402,30 @@ function prettyreal($n,$d) {
return number_format($n,$d);
}

function prettysigfig($a,$sigfig,$comma=',') {
function prettysigfig($a,$sigfig,$comma=',',$choptrailing=false) {
if ($a==0) { return 0;}
if ($a < 0 ) {
$sign = '-';
$a *= -1;
} else {
$sign = '';
}

$v = floor(-log10($a)-1e-12);

$v = floor(-log10($a));

if ($v+$sigfig < 0) {
return number_format(round($a,$v+$sigfig),0,'.',$comma);
return $sign.number_format(round($a,$v+$sigfig),0,'.',$comma);
} else {
$n = number_format($a,$v+$sigfig);
if (substr($n,-1)=='0') {
$n = substr($n,0,-1);
if ($choptrailing) {
$n = rtrim($n,'0');
$n = rtrim($n,'.');
} else {
if (floor(-log10($n)-1e-12) != $v) { //adjust for .009 -> .010 1 sig
$n = substr($n,0,-1);
}
}
return $n;
return $sign.$n;
}
}

Expand Down
2 changes: 2 additions & 0 deletions help.html
Original file line number Diff line number Diff line change
Expand Up @@ -1693,6 +1693,8 @@ <h2><a name="formatmacros">Format Macros</a></h2>
can only be used for display, not in calculations</li>
<li><b>prettyreal(number,decimals)</b>: Adds commas in thousands spaces of number, and rounds decimal to decimals places. Example: prettyreal(1234.567,2) will return "1,234.57". The result is a string, which
can only be used for display, not in calculations</li>
<li><b>prettysigfig(number,sigfigs,[comma,choptrailingzeros])</b>: Rounds number to sigfigs significant figures. By default it adds commas in thousands spaces; set comma to "" override.
Set choptrailingzeros to true to chop trailing zeros from the decimal part, even if significant.</li>
<li><b>makescinot(number,[decimals,format])</b>: Converts number to scientific notation. If provided, rounds mantissa to decimals places. Can specify format: "*" "E" as alternative to default cross.</li>
<li><b>prettytime(value,informat,outformat)</b>: Creates a nice representation of a time. Informat can be 'h', 'm', or 's' designating whether
value is hours, minutes, or seconds. Outformat can be any combination of these to specify output. For example, 'hm' will return "__ hours and __ minutes". Outformat
Expand Down

0 comments on commit 9375e4f

Please sign in to comment.