Significant Figures

From CodeCodex

function sigfigs($number,$sigfigs,$dec=".",$noround=false) {
    print "\$number = $number\n";
    print "\$sigfigs = $sigfigs\n";
    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 = $temp\n";
        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;
}