0

I am struggling with an issue where I have a string value '62.5816993713379' which when trying to convert to a float value gives me a different result.

Please consider the code below:

<?php

$str = '62.5816993713379';

echo floatval($str);

It returns me 62.581699371338 instead of 62.5816993713379

Example: http://codepad.org/WKeLwq7h

Can someone please help me fix this issue?

Regards

Phantom007
  • 2,079
  • 4
  • 25
  • 37
  • 2
    http://stackoverflow.com/questions/588004/is-floating-point-math-broken –  Dec 03 '16 at 02:24

2 Answers2

0

Because there is float-point cannot represent all the decimal number and it has the range. There are two solutions:

  • Keep your value as a string and use BC Math Function to do calculation.
  • Increase the ini_set('precision', 18); and cross finger that it can precisely represent your number that is out of its range previously.
invisal
  • 11,075
  • 4
  • 33
  • 54
0

You can use this

$str = '62.5816993713379';
echo sprintf ("%.14f", floatval($str));

14f is number of location till you want precision

Daniyal Syed
  • 543
  • 4
  • 22