Javascript – Unit conversion in Javascript

javascript

I have a question concerning number conversion i JS. I have a number like 1613841.93424 (meters) and I would like it to convert to 1.6 km instead. What JS function should I use for that? Thanks.

Best Answer

To convert from metres to kilometres, simply divide by 1000. To format a number using fixed-point notation, you can use the toFixed() method:

var km = 1613841.93424 / 1000;
alert(km.toFixed(1) + " km"); // 1613.8 km

Note: 1613841.93424 meters != 1.6 km (Source)