javascript - 如何将价格限制为小数点后两位

标签 javascript jquery decimal tofixed

我的 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">&pound;</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/

相关文章:

jquery TokenInput 库 - 如何重置为初始状态

python - 如何在python中提取float的十进制值

javascript - slider 拒绝将照片居中,强制左对齐

javascript - 使用脚本将属性设置为数据元素

javascript - 我可以在 jQuery 的查找中使用包含吗?

javascript mouseover/out 结合点击行为

javascript - Instagram Feed 光滑 slider

javascript - 即时更改 Controller

java - 十进制到二进制的转换

C: 十进制的 0x11