Version: 1.1
Type: Function
Category: Math Functions
License: GNU General Public License
Description: This function takes a number and a number of sigfigs to display it to and does it. You can specify an alternative decimal seperator to “.” (i.e. “,”) as the third argument and the fourth argument determines whether or not to round the last digit (the default is to round; send a NON-NULL value to prevent rounding).
The simplest call is as follows:
echo sigfigs($number,4);
echo sigfigs(pi(),5);
(The latter will show pi to 5sf)
function sigfigs($number,$sigfigs,$dec=".",$noround=false) { print "$number = $numbern"; print "$sigfigs = $sigfigsn"; if ($sigfigs < 1) { die("You have specified a number of significant figures to be displayed which is zero or negative. This is not possible."); } if (!$noround) { $sigfigs++; } for ($sfi=0;$sfi<=strlen($number) && $sfdone<$sigfigs;$sfi++) { $temp=substr($number,$sfi,1); print "$temp = $tempn"; if ($temp != "0" && $temp != $dec) { $after1stsf = true; } if ((($temp != "0") && ($temp != $dec)) || ($after1stsf && ($temp != $dec))){ $sfdone++; } if ($temp == "."){ $temp = $dec; } $output .= $temp; } if (substr($output,0,1) == $dec) { $output = "0".$output; } if (!$noround) { $splitbydp = explode($dec,$output); $numdps = strlen($splitbydp[1]); $output = round($output, ($numdps-1)); } return $output; }