2

I am trying to convert a scientific notation/exponential number.

For example :

I get from a API request this number : 4.96E-6

I want to convert it to his normal value 0.00000496.

I searched on the web and tryed a few codes like

$awb = "4.96e-6"; // tried with and without quotes
$format = sprintf($awb, "%e");
var_dump($format);

it returns : string(7) "4.96E-6"

$awb = 4.96e-6;
$format = sscanf($awb, "%e");
var_dump($format);

Is returning :

array(1) { [0]=> float(4.96E-6) }

Please what should I do ?

Thank you

John57
  • 33
  • 1
  • 4
  • Is it 496 or 596? – Andreas Dec 19 '17 at 18:38
  • @PatrickQ while I'm sure that this is a duplicate of something, the accepted answer on that is *awful*. – Sammitch Dec 19 '17 at 19:18
  • Do you want to convert this to a differently-formatted *string* for output, or to a *number* for calculations? – Sammitch Dec 19 '17 at 19:19
  • @Sammitch Yeah, I found what's probably a better one (https://stackoverflow.com/questions/5340283/convert-exponential-number-presented-as-string-to-a-decimal) after I picked that. Can't retract and then vote again though, so figured I'd just live my original close vote. – Patrick Q Dec 19 '17 at 19:20

1 Answers1

4

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

Drefetr
  • 412
  • 2
  • 7