Home » Php » php – Convert exponential number presented as string to a decimal

php – Convert exponential number presented as string to a decimal

Posted by: admin July 12, 2020 Leave a comment

Questions:

Let’s suppose I have a string that contains “7.2769482308e+01” (this number came from 3rd party software, I cannot control the format).

What is the cheapest way to convert it into decimal 72.769482308?

The only solution I can think of is to split decimal + exponential part and use multiplication. But may be there some built it function to do the same?

NOTE: Guys, yes, I’ve read Convert exponential to a whole number in PHP and Convert exponential number to decimal in php. And that questions are irrelevant, since they already have a number, but I have a string.

How to&Answers:

What about a simple cast to a float value ?

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

Answer:

I had success using number_format(1.2378147769392E+14, 0, '', '') which was originally provided by this question.

This also works when the value is a string, like so: number_format("1.2378147769392E+14", 0, '', ''). Go ahead, give it a try.

Answer:

Here is a very cheap one: $float = "7.2769482308e+01" + 0;