我想格式化我的数字,总是显示2小数点后,四舍五入适用的地方。

例子:

number     display
------     -------
1          1.00
1.341      1.34
1.345      1.35

我一直在用这个:

parseFloat(num).toFixed(2);

但是它把1显示为1,而不是1.00。


当前回答

对于现代浏览器,使用toLocaleString:

var num = 1.345;
num.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 });

指定区域设置标记作为控制十进制分隔符的第一个参数。对于点,使用例如English U.S. locale:

num.toLocaleString("en-US", { maximumFractionDigits: 2, minimumFractionDigits: 2 });

这使:

1.35

大多数欧洲国家使用逗号作为小数分隔符,所以如果你使用瑞典/瑞典地区:

num.toLocaleString("sv-SE", { maximumFractionDigits: 2, minimumFractionDigits: 2 });

它会给:

1, 35

其他回答

你没有告诉我们全部情况。

alert(parseFloat(1). tofixed(2))显示1.00在我的浏览器,当我粘贴到位置栏。 然而,如果你事后对它做了一些事情,它就会恢复。

alert(parseFloat(1).toFixed(2)) 变量数 = 2 document.getElementById('spanId').innerHTML = (parseFloat(num).toFixed(2) - 1) <span id=“spanId”></span>

shows 1 and not 1.00
(Math.round(num * 100) / 100).toFixed(2);

现场演示

var num1 = “1”; document.getElementById('num1').innerHTML =(Math.round(num1 * 100) / 100).toFixed(2); var num2 = “1.341”; document.getElementById('num2').innerHTML =(Math.round(num2 * 100) / 100).toFixed(2); var num3 = “1.345”; document.getElementById('num3').innerHTML =(Math.round(num3 * 100) / 100).toFixed(2); 跨度 { 边框:1px 实心 #000; 边距:5px; 填充:5px; } <span id=“num1”></span> <span id=“num2”></span> <span id=“num3”></span>

注意,它将四舍五入到小数点后2位,因此输入1.346将返回1.35。

<script src="https://code.jquery.com/jquery-3.6.0.js" integrity="sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.16/jquery.mask.min.js" integrity="sha512-pHVGpX7F/27yZ0ISY+VVjyULApbDlD0/X0rgGbTqCE7WFW5MezNTWG/dnhtbBuICzsd0WQPgpE4REBLv+UqChw==" crossorigin="anonymous"></script>


<input type="text" class = 'item_price' name="price" min="1.00" placeholder="Enter Price" value="{{ old('price') }}" step="">

<script> 
$(document).ready(function() {
    $('.item_price').mask('00000.00', { reverse: true });
});
</script>

RegExp -替代方法

在输入时,你有字符串(因为你使用解析),所以我们可以通过只使用字符串操作和整数计算得到结果

let toFix2 = (n) = > n.replace (d / \(?) -(+)。\ \ \ \ d (d + 1) / (_), s, i、d (r) = {> 让k= (+r[0]>=5)+ +d - (r==5 && s=='-'); 返回s +(+i+(k>99)) + "。"(k +(> 99%) ? ? 9: 00”(k≥0 " k " + k)); }) / /测试 console.log toFix2(“1”)); console.log (toFix2 1.341”()); console.log (toFix2 1.345”()); console.log (toFix2 1.005”());

解释

s is sign, i is integer part, d are first two digits after dot, r are other digits (we use r[0] value to calc rounding) k contains information about last two digits (represented as integer number) if r[0] is >=5 then we add 1 to d - but in case when we have minus number (s=='-') and r is exact equal to 5 then in this case we substract 1 (for compatibility reasons - in same way Math.round works for minus numbers e.g Math.round(-1.5)==-1) after that if last two digits k are greater than 99 then we add one to integer part i

在需要特定格式的地方,您应该编写自己的例程或使用库函数来完成所需的工作。基本的ECMAScript功能通常不足以显示格式化的数字。

关于舍入和格式的详细解释在这里:http://www.merlyn.demon.co.uk/js-round.htm#RiJ

作为一般规则,舍入和格式化应仅作为输出前的最后一步执行。过早地这样做可能会引入意想不到的大错误并破坏格式。