When using the Google API for calculating the distance between two location, it returns me the distance in string format.
For example: 1,585 KM, 54 KM etc.
I want it to be converted to a number. What is the best way to convert a string like 1,525 KM into a number like 1525?
Best How To :
If you are using the Google Maps Distance Matrix API, there is another field in the
distance object called
value that has a Number value. As documented in the API, the units is metres.
But to answer your original question, I would remove all non-digit characters from the string using a regular expression:
var dist = '1,525 KM';
dist = Number(dist.replace(/[^\d]/g, ''));