2014-02-16 27 views
0

我有一個帶有3個數字輸入字段的表單,用於輸入項目的價格,折扣和折扣價格,折扣價格最多顯示2位小數。將限制表單編號輸入到2個小數點

我看了看周圍淨,這裏SO想方設法數量限制爲2米小數的地方,我目前使用下面的代碼

代碼:

$('#ItemPercent').change(function(){ 
     var percent=$('#ItemPercent').val(); 
     var price=$('#ItemPrice').val(); 
     var discountpercent=percent/100; 
     var discountamt=price * discountpercent; 
     var discountamt=Math.floor(discountamt * 100)/100; 
     var discountamtrounded=discountamt.toFixed(2); 
     $('#ItemDiscountPrice').val(price - discountamtrounded); 
}); 

然而,即使使用上面的代碼,折扣價格不會顯示2位小數。

E.g

E.g

$129.99 <----ItemPrice 
10% <------Discount % 
$117.00000000000001 <------ ItemDiscountPrice. 

如果上面的例子就是我目前得到當我測試我的代碼運行。

我如何將顯示的數字限制在2個小數位?

回答

0

嘗試使用toFixed

$('#ItemDiscountPrice').val((price - discountamtrounded).toFixed(2)); 
+0

......我無法相信我錯過了!我正在考慮四捨五入,完全忘記了ItemDiscountPrice。 10分鐘過後,我會接受答案。謝謝! –

相關問題