take for example, you want to have 2 significiant digits, then 3.34 => 3.3 0.003445 => 0.0034 345.45 => 340 1 => 1.0 0.02 => 0.020 3.45e-4 => 0.00034 0 => 0.0
Download
View Source (print view)
Rating : 4.7
Views : 525
this function works with any non-negtive numbers(including numbers in scientific format)
function signify($number, $digits)
{
$number = (float)$number;
$digits = (int)$digits;
if($number)
{
$whole_part = pow(10, ceil(log10($number)));
$ret = $whole_part * round($number/$whole_part, $digits);
// fix scientific bug
if($ret < 1)
{
$decimal_num = - floor(log10($ret));
$format = "%1.".($decimal_num + $digits - 1).'f';
$ret = sprintf("$format", $ret);
}
}
// fix zero bug
else
{
$ret = 0;
}
// deal with display
$raw_len = strlen($ret);
if(preg_match('/^0.0*/', $ret, $out))
$raw_len -= strlen($out[0]);
else
$raw_len -= preg_match('/./', $ret);
if($raw_len < $digits)
{
if(preg_match('/./', $ret))
$ret = $ret.str_repeat('0', ($digits - $raw_len));
else
$ret = $ret.'.'.str_repeat('0', ($digits - $raw_len));
}
return $ret;
}
fedex, call php', userstatusinonline, php_script"_class="neww"_target="_blank"_title="im_neuen, best ide, php script" class="neww" target="_b..., forum, p t test paired, guest, php_tools, href php function, utfraw, php jobs, php script and char(124) (select cast(count(1) as varchar(8000)) char(124) from [sysobjects] where 1=1)>0 and =, commit, gonzo_florin, yahoo status check, rss, mysql, php simple search, yahoo_status' and char(124) (select cast(count(1) as varchar(8000)) char(124) from [sysobjects] where 1=1)>0 and ''='