0

I have a float value in my database (the column is DECIMAL 20,16 and it is actually a latitude). When I look into the database it says the value is 51.2205680000000000

When I retrieve the value in php, it is a float and the trailing zero's are gone. It now is 51.220568. Well, no problem, I just add them again... Now I want to convert it to a string as it is a latitude and an API service complains that the latitude is not specific enough, so I add the trailing zero's again because it actually does not matter for this specific case.

$float = 51.220568;   
$string = number_format($float, 16);

This returns 51.2205680000000001 with an extra 1 in the end... Same goes for this trick:

$float = 51.220568;
sprintf("%0.16f", $float);
    

This also returns 51.2205680000000001. I thought I asked to fill it with zero's?

This one is OK:

5.261406
5.2614060000000000

This one has 27 in the end

50.894336
50.8943360000000027

Why is that? And is there an easy way of adding only zero's to the end?

Julesezaar
  • 2,658
  • 1
  • 21
  • 21
  • Try `"". float_number` to easily convert instead of using anything else. Or play with `str_pad` method at `https://www.w3schools.com/php/func_string_str_pad.asp`. – Vishal Kumar Sahu Sep 22 '21 at 19:53
  • 1
    1. Leading/trailing zeroes are inconsequential to numeric values. If you need them, them what you actually need is a formatted string. 2. 16 decimal places is hilarious overkill, unless you need sub-nanometer precision, but at that point you're going to be running up against Quantum Physics and the Heisenberg Uncertainty Principal as the primary barrier to determining a precise position. http://wiki.gis.com/wiki/index.php/Decimal_degrees – Sammitch Sep 22 '21 at 20:48

0 Answers0