可能重複:
How do you round to 1 decimal place in Javascript?在javascript舍入的數到小數點後
下面的代碼,顯示所覆蓋的總距離,特定的路線上,在谷歌地圖顯示。我設法將數字從公里轉換爲英里。以下是該功能的代碼:
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total *0.621371/ 1000.
document.getElementById('total').innerHTML = total + ' mi';
總數顯示爲41.76483039399999 mi
。如何將total
四捨五入爲小數點後一位?
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Number/toFixed 變種N = 123.123; n.toFixed(1); // 123.1 –