3

I have a floating point number in exponential format i.e. 4.1595246940817E-17 and I want to convert it into decimal number like 2.99 etc.

Any help will be appreciated.

format_number() sprintf() don't seem to be working for me.

ajreal
  • 46,720
  • 11
  • 89
  • 119
Saqib
  • 1,283
  • 1
  • 13
  • 16
  • Exponential or decimal are just ways of showing a human-readable form of the number. I am not really sure of what you want.. – ssice Dec 16 '10 at 14:14
  • 1
    I can't figure out where 2.99 comes from (you might want to tell us what you _really_ expect), but are you expecting __number_format(4.1595246940817E-17, 2)__ to produce __4.15__? If it did, your calculations would be off by many orders of magnitude (the correct result would be __0.00__). – GZipp Dec 16 '10 at 15:08
  • Yes I am expecting 4.1595246940817E-17 to produce 2.99. Here is my situation. Allocation Size: 65536 bytes = 65536/1024 = 64KB. Allocation units: 49107. Total size= 49107*64 = 3142848KB = 3069.18MB= 2.99GB – Saqib Dec 16 '10 at 15:42
  • Here is my code
    This produces 2.99 but if I get realtime values from registry/snmp trap etc.. then it produces 4.1595246940817E-17.
    – Saqib Dec 16 '10 at 15:50
  • That's completely different from your question, then. Try the solutions given by ajreal and Russell Dias, below – GZipp Dec 16 '10 at 16:02

4 Answers4

4

You need a better math extension like BC Math, GMP... to handle the more precise precision.

Limitation of floating number & integer

ajreal
  • 46,720
  • 11
  • 89
  • 119
  • Yes I am expecting 4.1595246940817E-17 to produce 2.99. Here is my situation. Allocation Size: 65536 bytes = 65536/1024 = 64KB. Allocation units: 49107. Total size= 49107*64 = 3142848KB = 3069.18MB= 2.99GB – Saqib Dec 16 '10 at 15:38
  • @user544836 - does it a maths ? If so, try the math extension, If **NOT** ... not sure what can be done – ajreal Dec 16 '10 at 15:41
  • @Saqib - ic, you should does this instead `php -r "echo 49107*64/1024/1024;"`, preset the Gb instead of let it overflow... – ajreal Dec 16 '10 at 16:07
1

Using the BC Math library you can bcscale() the numbers to a predetermined decimal, which sets the parameter for future calculations that require arithmetic precision.

bcscale(3);
echo bcdiv('105', '6.55957'); // 16.007
Russell Dias
  • 70,980
  • 5
  • 54
  • 71
1

You could remove the decimal point ($x is your number):

$strfloat = strtolower((string)($x));
$nodec = str_replace(".", "", $x);

Then extract the exponential part.

list($num, $exp) = explode("e", $nodec);
$exp = intval($exp);

Then you have the decimal, and the number, so you can format it:

if($exp < 0) return "0." . ("0" * -($exp + 1)) . $num;
if($exp == 0) return (string)$x;
if($exp > 0) return $num . ("0" * $exp);

This doesn't add precision though, just extra zeroes.

Thomas O
  • 6,026
  • 12
  • 42
  • 60
1

Here's a solution using BC Math, as suggested by ajreal and Russell Dias:

$au = 65536; 
$auk = bcdiv($au, 1024);
$totalSize = bcdiv(bcmul(49107, $auk), bcpow(1024, 2), 2); 
echo $totalSize . "\n"; 

// echos 2.99
GZipp
  • 5,386
  • 1
  • 22
  • 18