Convert Exponential to a Whole Number in PHP

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 presented as string to a decimal

What about a simple cast to a float value ?

$string = "7.2769482308e+01";
$float = (float) $string;

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.

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 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- floating point number shown in exponential form

The exponential form is the internal one use by every (?) programming language (at least CPUs "sees" floats this way). Use sprintf() to format the output

echo sprintf('%f', $a);
// or (if you want to limit the number of fractional digits to lets say 6
echo sprintf('%.6f', $a);

See Manual: sprintf() about more information about the format parameter.

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";


Related Topics



Leave a reply



Submit