Convert exponential number presented as string to a decimal
What about a simple cast to a float value ?
$string = "7.2769482308e+01";
$float = (float) $string;
Convert exponential to a whole number in PHP
number_format(1.2378147769392E+14,0,'','')
However, for working with large numbers, if you want to keep precision, you should look into BCMath extension
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
Convert exponential number to decimal in php
You need a better math extension like BC Math, GMP... to handle the more precise precision.
Limitation of floating number & integer
How to convert Exponentials to Decimals in PHP
EDIT: Here is some PHP magic:
$stringval = "12e-3";
$numericval = 0 + $stringval;
From the PHP docs:
If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer. In all other cases it will be evaluated as a float.
If you need a more flexible format (e.g. extract four numbers from the same string), use sscanf
like this:
$stringval = "12e-3";
$numericval = sscanf($stringval, "%f")[0];
echo $numericval;
How to convert exponential number(SimpleXML object string) to decimal in PHP
The value you're passing to round
needs to be cast to float before the function runs, either explicitly by your code, or implicitly by the PHP engine. If you pass in a string, it will be coerced to a float automatically, but if you pass in an object - such as the SimpleXMLElement
you have here - it gets cast to an int instead.
This is because of an issue with PHP's internals, filed as PHP bug #53033 (and a few near-duplicates), which it looks like Nikita Popov has fixed for PHP 7.3. Basically, the engine had no way to ask an object with custom conversion code for "a number", so it could not choose smartly between a float and an int.
In the meantime, explicitly casting to float, e.g.$value = round( (float)$my_object, $precision );
will avoid the issue.
Casting to string would also work, because the engine knows it should convert a string to a float in this situation, so it can be a useful general habit to add (string)
in front of elements and attributes as you extract them from SimpleXML. This also avoids other issues, such as accidentally trying to serialize the object in a session, or keeping the XML in memory for longer than necessary because it's accessible from some value object.
php converting exponetial value having exponent 12 to decimal
That's because float precision
setting is default 14
. To change this, use ini_set()
, for example.Then you'll be able to get proper values. Sample:
$strVal = "1234567890.123456789";
//float(1234567890.1235), because
//default precision is 14:
var_dump((double)$strVal);
//float(1234567890.123456717)
ini_set('precision', 19);
var_dump((double)$strVal);
This is not only about decimal precision, but about float precision
:
$strVal = "1234567890123456789";
var_dump((double)$strVal);//float(1.2345678901235E+18)
ini_set('precision', 19);
var_dump((double)$strVal);//float(1234567890123456768)
Also, important note - it seems that trying to overcome precision in your case is an attempt to resolve symptoms, not the problem. So you should choose correct data model rather than try to solve this "problem".
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.
Related Topics
Laravel 3:Looking for Explanation How to Use the Model
Mysqli_Query(): Couldn't Fetch MySQLi Error
Is There a Built-In Function or Plugin to Handle Date Formatting in JavaScript
Ftp_Put Is Corrupting My Movies After Transfer, Is It Wrong
PHP and MySQLi - Cannot Pass Parameter 2 by Reference In
PHP Re-Order Array of Month Names
Store_Result() and Get_Result() in MySQL Returns False
PHP on Godaddy Linux Shared Trying to Send Through Gmail Smtp
Keeping Url Parameters During Pagination
Show Stock Status Next to Each Attribute Value in Woocommerce Variable Products
How to Display PHP & HTML Source Code on a Page
Call to Undefined Method Domdocument::Getelementsbyclassname()
Multiple Replace (Probably Preg_Replace) of Same String with Array