Google MAP - Exchanging comma per point using native JS

Asked

Viewed 38 times

0

Hello, I’m using a google map function to return 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 with a comma (2,7), have two questions...

  • How to swap the comma for a point, so I can complete the calculation.
  • It is possible to display after point 3 digits.

Thank you.

1 answer

1


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

var num = '12,3';

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

Explanation

  • The replace makes substitutions, in this case the , for ..
  • Passing the replaced text to float (number with comma) it is possible to use the method toFixed.
  • toFixed(3) says that the number must be to 3 decimal places.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.