Google MAP - Switch commas by using native JS

0

Hello, I'm using a google map function to return the distance between zip codes.

function callback(response, status) {
                    if (status == google.maps.DistanceMatrixStatus.OK) {
                        kmDistancia = (response.rows[0].elements[0].distance.text).split(" ",1);
                        tempoDistancia = response.rows[0].elements[0].duration.text;
                    }
                }

The kmDistance , always returns comma (2,7) , I have two questions ...

  • How to change the comma by period, so I can complete the calculation.
  • You can display after the 3 digit point.

Thank you.

    
asked by anonymous 09.09.2016 / 18:22

1 answer

1

This operation is very simple, just use replace and toFixed .

var num = '12,3';

console.log(parseFloat(num.replace(',', '.')).toFixed(3));

Explanation

  • replace performs substitutions, in this case , by . .
  • Passing replaced text to float (comma number) you can use the toFixed method.
  • toFixed(3) says that the number should be displayed with 3 decimal places.
09.09.2016 / 18:31