我的 HTML
<div class="product-line">
<a href="#" alt="close" class="btn-close" title="Remove"><img alt="remove" src="img/close.png" /></a>
<input class="input-text" name="product-code" type="text" placeholder="Product Code" />
<input class="input-text" name="product-quantity" type="text" placeholder="Quantity" />
<input class="input-text" name="product-discript" type="text" placeholder="Discription of Product" disabled />
<label class="label-sign">£</label>
<input class="input-text price" name="product-price" type="text" placeholder="RRP Price" disabled />
<br>
</div>
我的 JS 代码行
price = $(this).parent("div.product-line").find("input[name=product-price]").val( Number(price).toFixed(2) * quantity )
基本上,如果我乘以 40.2 数量 3,我会得到类似 120.600000000.... 我怎样才能将其限制为小数点后两位。
数据通过 JSON 传入(由其他人创建)。
我是一个JS新手
最佳答案
只需将 toFixed
移动到乘法的输出即可。
.val( ( Number(price) * quantity ).toFixed(2) );
关于javascript - 如何将价格限制为小数点后两位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23498549/