1

I have a string like this:

  9.018E-14

Now I want to convert to this to the normal decimal numbers.

php geek
  • 209
  • 1
  • 4
  • 13

3 Answers3

4

MyGeekPal has a nice article on it.

Code:

<?php
$total_time = 2.8848648071289E-5;

echo exp2dec($total_time);

function exp2dec($number) {
    preg_match('/(.*)E-(.*)/', str_replace(".", "", $number), $matches);
    $num = "0.";
    while ($matches[2] > 0) {
        $num .= "0";
        $matches[2]--;
    }
    return $num . $matches[1];
}
?>
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
2

If your input is a float

If you have $number = 0.00023459 then printing this value in PHP will probably result in this exponential format. It doesn't mean the variable is stored that way; it's just an output artefact.

Use printf to work around this and gain control over your numeric output.


If your input is a string

Why the complexity?

$matches = Array();
if (preg_match('/(.*)E-(.*)/', $number, $matches)) {
   $number = $matches[1] * pow(10, -1*$matches[2]);
}

Though you can tighten up the regex a bit:

$matches = Array();
if (preg_match('/(\d+(?:\.\d+)?)E(-?\d+)/i', $number, $matches)) {
   $number = (float)$matches[1] * pow(10, (int)$matches[2]);
}

Live demo

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

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;
Stefano Sanfilippo
  • 32,265
  • 7
  • 79
  • 80
  • There's even simpler methods: `$floatval = floatval($stringval);` or `$floatval = (float) $stringval;` but what are you wittering about integers for when this is clearly a float? – Mark Baker May 07 '13 at 13:39
  • Simpler? They are just as simple, maybe longer to write (and a bit more explicit). But why downvote? – Stefano Sanfilippo May 07 '13 at 13:41
  • Downvoted because you're wittering on about integers, why are integers relevant? – Mark Baker May 07 '13 at 13:42
  • The OP never said he/she wanted a float number as result. If the number can fit inside a integer, why avoid using it? – Stefano Sanfilippo May 07 '13 at 13:45
  • 1
    9.018E-14 is a float unless you round it to an integer, losing all the precision... you've given an answer that is complex, unnecessary, and returns a result that bears no resemblence to the original number.... what value do you get from `$intval = 1 + $stringval;`? A result that's had 1 added to it.... where does OP say he wants to add a 1 to his value? – Mark Baker May 07 '13 at 13:50
  • That was an example: I don't think the OP would bother asking if it was all about converting one number. Good point about the 1 (mistype, edited), but -1 on floating point: `1e2` and `100` are equivalent and the associated value can be represented both by an integer and a floating point number. Read the paragraph I posted in the answer: the number is converted to floating point, unless it's integer and it fits inside the integer range: no loss, apart from the finite precision, but that cannot be avoided. I am aware that other answers are acceptable, but I don't see a reason for downvoting. – Stefano Sanfilippo May 07 '13 at 13:57
  • Perhaps if you modify the references to $intval because that's misleading – Mark Baker May 07 '13 at 14:06