8

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.

Community
  • 1
  • 1
zerkms
  • 249,484
  • 69
  • 436
  • 539

4 Answers4

16

What about a simple cast to a float value ?

$string = "7.2769482308e+01";
$float  = (float) $string;
hsz
  • 148,279
  • 62
  • 259
  • 315
  • Lol, surprisingly simple... Never know that php can recognize string exponential format in casting. Is this behaviour documented? – zerkms Mar 17 '11 at 14:21
  • 1
    @zerkms Yeah. http://php.net/manual/en/language.types.string.php#language.types.string.conversion and http://php.net/manual/en/language.types.float.php – Wiseguy Mar 17 '11 at 14:23
  • Yes, something exists in documentation: http://ru.php.net/manual/en/language.types.string.php#language.types.string.conversion – zerkms Mar 17 '11 at 14:23
  • 1
    @zerkms: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion – Jon Mar 17 '11 at 14:23
  • 1
    Documented at http://uk2.php.net/manual/en/language.types.string.php#language.types.string.conversion with the example of "-1.3e3" – Mark Baker Mar 17 '11 at 14:24
  • @hsz: definitely not on that juggling page though ;-) – zerkms Mar 17 '11 at 14:25
13

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.

Community
  • 1
  • 1
jwhat
  • 2,042
  • 23
  • 29
  • 7
    Ouch, harsh words... Have you even tried this? Because this works both when the value is and is not a string. – jwhat Jun 14 '12 at 00:59
  • any reason to perfer over a solution that almost twice shorter and more readable: `(float) $string` ? PS: they are not harsh, it wasn't an answer in its initial edition PPS: `number_format` accepts the number as a first argument so the string is being converted implicitly PPPS: the result is a string, so another conversion need to be performed to get the decimal/float – zerkms Jun 14 '12 at 02:52
  • somehow $float = (float) $string; is twice shorter than $float = number_format($string, 0, '', '').. lol.. OP had a bit too much coffee I think. Good answer btw! Just upvoted – Robert Sinclair Dec 07 '19 at 00:45
1

Making this dynamic with respect to no. of digits after E would be much better.

Grab those digit precisions using explode and use sprintf to format them.

Snippet:

<?php

function getFormattedDecimal($str){
    $precision = explode("e", strtolower($str));
    $precision = substr($precision[1], 1); // remove + and - operators
    return sprintf("%.".$precision."f", $str);
}

Online Demo

Note: sprintf may round decimal places depending upon the decimal style.

nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

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

steloh
  • 11
  • 3