JS - 格式编号,带小数点后2位未舍入 [英] JS - Format number with 2 decimal not rounded
本文介绍了JS - 格式编号,带小数点后2位未舍入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我会格式化一个带有2位小数的数字而不进行舍入。
所以我排除了toFixed()函数。
I would format a number with 2 decimal places without rounding. So I excluded the toFixed() function.
我试过这种方式
a = 1,809999
b = 27,94989
a = Math.floor(a * 100) / 100; --> 1,8
b = Math.floor(b * 100) / 100; --> 27,94
OR
a = Number(a.toString().match(/^\d+(?:\.\d{0,2})?/)); --> 1,8
b = Number(b.toString().match(/^\d+(?:\.\d{0,2})?/)); --> 27,94
不幸的是,a的第二个小数是零,这被删除了,我怎么能保持它并且= 1.80?
谢谢
Unfortunately, the second decimal of a is zero, and this was deleted, how could I do to keep it and have a = 1.80? Thank you
推荐答案
(Math.floor(a * 100) / 100).toFixed(2);
使用toFixed(2)!
With toFixed(2) !
JSFIDDLE DEMO
这篇关于JS - 格式编号,带小数点后2位未舍入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文