Convert a string containing a number in scientific notation to a double in PHP
PHP is typeless dynamically typed, meaning it has to parse values to determine their types (recent versions of PHP have type declarations).
In your case, you may simply perform a numerical operation to force PHP to consider the values as numbers (and it understands the scientific notation x.yE-z
).
Try for instance
foreach (array("1.8281e-009","2.3562e-007","0.911348") as $a)
{
echo "String $a: Number: " . ($a + 1) . "\n";
}
just adding 1 (you could also subtract zero) will make the strings become numbers, with the right amount of decimals.
Result:
String 1.8281e-009: Number: 1.0000000018281
String 2.3562e-007: Number: 1.00000023562
String 0.911348: Number: 1.911348
You might also cast the result using (float)
$real = (float) "3.141592e-007";
convert a scientific notation/exponential number
You can use the number_format() function; e.g.
<?php
$awb = "4.96e-6";
print number_format($awb, 10);
?>
Will result in the output: 0.0000049600.
See: http://php.net/manual/en/function.number-format.php
PHP how to convert number exponential number to string?
printf
and friends will do this:
<?php
$awb = 2.01421700079E+14;
$str = sprintf("%d", $awb);
var_dump($str);
Output:
string(15) "201421700079000"
There obviously isn't enough information in your original number to get any more precision than that.
scientific notation to normal decimal output conversion
I found a way to do it myself and I am posting a quick answer.
function realval($v) {
$f = (string)number_format($v, 10, '.', ''); // 10000.0000010000
if(strpos($f, '.') !== false) { // if it's a decimal (if not, print as is)
$f = rtrim($f, "0"); // 10000.000001
$f = rtrim($f, "."); // to prevent 10000. like things.
}
return $f; // string format
}
This does what I needed. I wanted a more decent way to do so, but not figured out yet.
incorrect number from scientific notation in PHP
Note that the double
floating point format has a precision of about 15.5 decimals. All other displayed decimals are extrapolated. And as always, extrapolation is a dangerous game.
Or in other words, 3.2486509223274E+19
represents an interval, roughly from 3.24865092232739984E+19
to 3.24865092232740024E+19
. Any integer in that interval has to count as "correct" extrapolation.
BC math string into scientific notation
Count how many zeroes there are.
Note that because you are using big numbers, you have to work on them as strings. So...
if( preg_match("/^0\.0*/",$value,$m)) {
$zeroes = strlen($m[0]);
$value = substr($value,$zeroes,1)
.rtrim(".".substr($value,$zeroes+1),"0.")
."E-".($zeroes-1);
}
elseif( preg_match("/(\d+)(?:\.(\d+))?/",$value,$m)) {
$zeroes = strlen($m[1]);
$value = substr($value,0,1)
.rtrim(".".substr($m[1],1).$m[2],"0.")
."E+".($zeroes-1);
}
// else 1 <= number < 10, so no transformation needed
Test cases:
- 1000000 => 1E+6
- 1234.5678 => 1.2345678E+3
- 0.9 => 9E-1
- 0.123 => 1.23E-1
- 0.00000011111122222 => 1.1111122222E-7
Related Topics
PHP Allocate Color Without Image Resource
How Is MVC Supposed to Work in Codeigniter
How to Post a Large Amount of Data Within PHP Curl Without Memory Overhead
Decoding Numeric HTML Entities via PHP
How to Keep the Chinese or Other Foreign Language as They Are Instead of Converting Them into Codes
PHP Adds Keys to Decoded and Then Encoded JSON Data
Sort Array by Length and Then Alphabetically
How to Change Default Redirect Url of Laravel 5 Auth Filter
Check If a Remote Page Exists Using PHP
Is This a PHP Date() Bug, or Is There Something Wrong with My Code
Why Are $_Server["Php_Auth_User"] and $_Server["Php_Auth_Pw"] Not Set
Phpexcel Reader -- Help Required
Apple Push Notification with Sending Custom Data
Using Websocket on Apache Server
How to Make Laravel Eloquent "In" Query