JavaScript数学,在Jquery模板中四舍五入到小数点后两位
JavaScript math, round to two decimal places in Jquery template
我有一些代码
{{if commission}}
<td>${profit - commission}</td>
{{else}}
<td>${profit}</td>
{{/if}}
profit = 5
;
commission = 2.145
result = 2.855999999999
我需要2.856
请帮帮我
我尝试使用(${profit - commission}).toFixed(2)
-但它不工作。
直接使用toFixed(3)
,它将选择点值后面的3位数字
var s=2.855999999999;
alert(s.toFixed(3))
OP
: 2.856
var result = 2.855999999999;
result = result.toFixed(2); //returns string fixed to 2 decimal places
result = parseFloat(result);//returns double 2 decimal places
alert(result);
在jQuery模板中使用
parseFloat应该在${}
$ {parseFloat(值).toFixed (2)}
{{if commission}}
<td>${parseFloat(profit - commission).toFixed(2)}</td>
{{else}}
<td>${profit}</td>
{{/if}}
您可以使用:
var result = 2.855999999999;
result = Math.round(result * 1000) / 1000;
console.log(result ); // ----> 2.856
演示工作在这里工作
可以使用Math.round()
var num = 2.855999999999
num = Math.round(num * 1000) / 1000
alert(num);
D
试试这个:
parseFloat(${profit - commission}).toFixed(3);
相关文章:
- 将数字四舍五入到小数点后两位
- 将数字四舍五入到小数点后两位,以设置货币格式
- 用Javascript显示价格,小数点后两位
- 小数点后两位四舍五入-找不到正确的公式
- 将 JavaScript 数字转换为小数点后两位
- 将数字舍入到小数点后两位,并删除任何其他小数点
- 在谷歌图表中以小数点后两位显示数字
- 在 Javascript 中难以将 json 对象值四舍五入到小数点后两位
- 在asp.net文本框中键入时自动添加一个点(.)(将文本框格式设置为小数点后两位)
- 如何将减去的值四舍五入到小数点后两位
- JavaScript数学,在Jquery模板中四舍五入到小数点后两位
- JS四舍五入到小数点后两位
- javascript数学,以.05为增量四舍五入到小数点后两位
- 仅当数字小于小数点后两位时添加.00 (toFixed)
- 如何在java脚本中进行小数点后两位的取整
- 四舍五入到小数点后两位,javascript
- 查找小于小数点后两位的数字的REGEXP
- 将计数 javascript 程序设置为小数点后两位
- 在文本框的按键中限制为小数点后两位
- 如何在Javascript中四舍五入小数点后两位