始终显示至少两位小数 [英] Always display at least two decimal places
本文介绍了始终显示至少两位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想格式化一个数字,使其始终至少有两位小数。
I want to format a number so that it always have at least two decimal places.
样本:
1
2.1
123.456
234.45
输出:
1.00
2.10
123.456
234.45
推荐答案
您可以修复为2或当前位数;
You could fix to 2 or the count of current places;
var result = num.toFixed(Math.max(2, (num.toString().split('.')[1] || []).length));
这篇关于始终显示至少两位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文