note 36055 deleted from function.number-format by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 01:34:08 +0000 |
| Subject: | note 36055 deleted from function.number-format by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57358@lists.php.net to get a copy of this message | ||
Note Submitter: Andi
----
It seems, as if PHP does not provide a function to display floating point number as exponetial
numbers; either I'm too stupid or it is simply not there. The following funciton does the job:
$float is the floating point number
optional $comma defines the number of decimals to display.
function floatToExp($float, $comma=4)
{
if ($float == 0)
{
return(number_format(0,$comma)."e+00");
}
if ($float < 0)
{
$float = abs($float);
$minus = "-";
}
else $minus = "";
if ($float >= M_E)
{
$count = 0;
while (strlen(floor($float)) > 1)
{
$float /= M_E;
$count++;
}
if (strlen($count)<2) $count = "0".$count;
return ($minus.number_format($float,$comma)."e+".$count);
}
else
{
$count = 0;
while (floor($float) < 1)
{
$float *= M_E;
$count++;
}
if (strlen($count)<2) $count = "0".$count;
return ($minus.number_format($float,$comma)."e-".$count);
}
if (strlen($count)<2) $count = "0".$count;
}