2017-04-07 71 views
1

當我在Javascript中做了一些簡單的算術時,出現了一些錯誤。我不想要這個,但我不知道如何處理它。 這是例如:如何處理這個舍入錯誤

38.8 * 3 => 116.39999999999999

enter image description here

+0

您需要將數字四捨五入到最接近的整數。檢查此:http://stackoverflow.com/questions/6968042/how-can-i-round-to-whole-numbers-in-javascript –

+0

你可以像這樣四捨五入數字 - var n = num.toFixed(2) ; –

回答

0

使用本

function multiply (a, b) { 
    exp = b.toString().length - 2; 
    function makeInt (num) { 
     return num * Math.pow(10, exp); 
    } 
    function makeFloat(num) { 
     return num/Math.pow(100, exp); 
    } 
    return makeFloat(makeInt(a) * makeInt(b)); 
} 

var result = multiply(38.8,3) 

console.log(result); 

https://jsfiddle.net/v51s34st/

0

使用Math.ceil(38.8 * 3)或數學。地板(38.8 * 3)

0

您可以使用
parseFloat((38.8*3).toFixed(2))