note 36055 added to function.number-format
| From: | Andi at rn2 dot php dot net | Date: | Wed, 24 Sep 2003 09:28:20 +0000 |
| Subject: | note 36055 added to function.number-format | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-57035@lists.php.net to get a copy of this message | ||
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;
}
----
Manual Page -- http://www.php.net/manual/en/function.number-format.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36055
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36055&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36055&report=yes