2016-12-30 56 views
-1

當我使用P = 10000,R = 15,N = 60下......

var x = parseFloat((Math.round(r/12/100*p/(1-Math.pow(1+ +(r/12/100),-n))*100)/100).toFixed(2)); 

X = 237.9而不是237.90

如果P,R的組合和正導致許多不XX.X $」,那麼代碼的摘錄格式能正常工作......即到小數點後2位。

但爲何它顯示237.9,而不是237.90?

+0

嗨巴爾馬 - 你可以請參考我對相同問題的回答嗎?謝謝 –

+0

點擊鏈接。 – Barmar

+0

@Barmar這裏的問題並沒有錯誤地使用'toFixed'(因爲N [確實意味着正好有N個十進制數字](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/ Global_Objects/Number/toFixed)),結果被轉換回浮點數。 – Frxstrem

回答

0
var x = parseFloat((Math.round(r/12/100*p/(1-Math.pow(1+ +(r/12/100),-n))*100)/100)).toFixed(2); 

p=10000,r=15, n=60; 
 

 
var x = parseFloat((Math.round(r/12/100*p/(1-Math.pow(1+ +(r/12/100),-n))*100)/100)).toFixed(2); 
 

 
console.log(x)

所有操作之後,添加toFixed。你需要串,基本上...

1

當你調用number.toFixed(2),你確實得到了數字的字符串表示要以兩位數字:

var number = 237.9; 
number.toFixed(2);    // '237.90' 

但是,當你再使用這個parseFloat,你把它轉換多項再次;因爲一些不包含零,顯示的號碼信息,最後的零被丟棄,因爲它是打印:

parseFloat(number.toFixed(2)); // 237.9 

爲了避免這種情況,根本就不是你的字符串轉換回浮動,但使用它作爲一個字符串。

相關問題