[英]JavaScript function is not returning any boolean value or ticket calculation is wrong
[英]Is there any function to fix the returned value of a math calculation in javascript?
我正在做這個數學運算,即 (a - b) / a = number 但 javascript 沒有返回正確的值
這是我的代碼
6200000 - 5800000 / 6200000
它應該返回 0.06451 但它返回
5799998.931034483
我怎樣才能只得到 0.06?
嘗試使用括號首先評估減法和toFixed()
:
var n = ((6200000 - 5800000) / 6200000).toFixed(2); console.log(n);
使用括號
(6200000 - 5800000) / 6200000
除法發生在減法之前。為避免它,請使用括號
如果你想要兩位十進制使用((6200000 - 5800000) / 6200000).toFixed(2)
((6200000 - 5800000) / 6200000).toFixed(2)
數學運算的順序。
它將在減法之前運行除法。
要增加減法的順序,您可以將其放在括號中。
(6200000 - 5800000) / 6200000
要將其縮小到小數點后 2 位,請添加toFixed
function。
((6200000 - 5800000) / 6200000).toFixed(2)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.