0

In php, I gather geocoords and round them. The results dumped are as expected:

Array(
    [0] => Array
        (
            [0] => 37.76073
            [1] => -122.439948
        )

    [1] => Array
        (
            [0] => 37.973535
            [1] => -122.531087
        )

    [2] => Array
        (
            [0] => 37.973535
            [1] => -122.531087
        )    
)

( The duplicates are also expected. ) Then I pass them into javascript for use on a map:

var locations = <?php echo json_encode( $geo_locations ); ?>;

But when I log locations to the console, the values have changed for the 2nd element:

0: (2) [37.76073, -122.439948]
1: (2) [37.97368106291033, -122.53094093708967]
2: (2) [37.973535, -122.531087]

I've never seen this behavior before. Is it due to json_encode? Or something else?

iAmOren
  • 2,760
  • 2
  • 11
  • 23
shanebp
  • 1,904
  • 3
  • 17
  • 28

1 Answers1

1

This doesn't have anything to do with JSON. It's how floating point numbers are handled in computers.

See also: Is floating point math broken?

Brad
  • 159,648
  • 54
  • 349
  • 530