note 23038 added to ref.bc
| From: | benjcarson@php.net | Date: | Sun, 07 Jul 2002 21:00:03 +0000 |
| Subject: | note 23038 added to ref.bc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-32716@lists.php.net to get a copy of this message | ||
In addition to my last note, here are a quick pair of functions to convert exponential notation
values into bcmath-style number strings:
// exp2int converts numbers in the
// form "1.5e4" into strings
function exp2int($exp) {
list($mantissa, $exponent) = spliti("e", $exp);
list($int, $dec) = split("\.", $mantissa);
bcscale ($dec);
return bcmul($mantissa, bcpow("10", $exponent));
}
// float2exp converts floats into exponential notation
function float2exp($num) {
if (0 == $num) { return "0E1";}
list($int, $dec) = split("\.", $num);
// Extract sign
if ($int[0] == "+" || $int[0] == "-") {
$sign = substr($int, 0,1);
$int = substr($int, 1);
}
if (strlen($int) <= 1) { // abs($num) is less than 1
$i=0;
for ($i=0; $dec[$i]=='0' && $i < strlen($dec); $i++);
$exp = -$i-1;
$mantissa = substr($dec,$i,1).".".substr($dec,$i+1);
} else { // abs($num) is greater than 1
$i=0;
for ($i=0; $int[$i]=='0' && $i < strlen($int); $i++);
$exp = strlen($int)-1 - $i;
$mantissa = substr($int,$i,1).".".substr($int,$i+1).$dec;
}
return ($sign . $mantissa . "E" . $exp);
}
--
http://www.php.net/manual/en/ref.bc.php
http://master.php.net/manage/user-notes.php?action=edit+23038
http://master.php.net/manage/user-notes.php?action=delete+23038
http://master.php.net/manage/user-notes.php?action=reject+23038