-5

How to make number like 3.0000000054978E+38 to 3.00 in PHP?

Many thanks!

hakre
  • 193,403
  • 52
  • 435
  • 836
Acubi
  • 2,793
  • 11
  • 41
  • 54

4 Answers4

2

You cannot use round to solve this since it is a number in scientific notation. You can, however, use substr:

$i = 3.0000000054978E+38;
$i = substr($i, 0, 2); // $i is now the string 3.00
echo( number_format($i+1,2) );  // Will output 4.00
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • Round does work for scientific notation. There's nothing wrong with round and floats. – hakre May 01 '12 at 12:56
  • `$i = 3.0000000054978E+38; echo( round( $i, 2) );` That outputs 3.0000000054978E+38 on Windows, not 3.00 – Jeff Lambert May 01 '12 at 12:57
  • @Watcher: Sure, because it does what it has been documented for. But it's obvious that TS is not asking for string manipulation, isn't it? – hakre May 01 '12 at 12:58
  • OP asked for output of 3.00, AFAIK that's the only way to do it – Jeff Lambert May 01 '12 at 12:59
  • @watcher: Yes if you treat the input as a constant, yours ain't wrong, but [it's not the only way to do it, round works perfectly well](http://stackoverflow.com/a/10397974/367456). – hakre May 01 '12 at 13:03
2

In case you are looking for the small fraction of your number being outputted in a formatted fashion:

$number = 3.0000000054978E+38;
printf('%.2f', $number / 1E+38); # 3.00
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I'm not really sure it's better in the eye of the TS, it's still undefined what Acubi is specifically looking for. – hakre May 01 '12 at 13:04
1

You can just use round, as in round($floating_number, 2).

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

sprintf() always gives you the specified number of decimal points, if you require.

sprintf('%0.2f', 3.0000000);

Would display 3.00, if you echo it.

Lion
  • 18,729
  • 22
  • 80
  • 110