我正在尝试将价格格式的数字格式化为x,xxx.xx的格式,例如1,000.55,数据库中的数字是一个十进制(8,2),即1000.55,但是当我尝试在onLocaleString上使用时这个数字是行不通的。
这是我在vue中使用的功能
formatProdPrice(value) {
return value.toLocaleString(['en-US', [{minimumFractionDigits: 2, maximumFractionDigits: 2}]]);
}
Run Code Online (Sandbox Code Playgroud)
这是我用的地方
formatProdPrice($page.price.price)
Run Code Online (Sandbox Code Playgroud)
预期的输出为1,000.55,但现在的实际输出为1000.55。我究竟做错了什么?
小智 5
您也可以使用Intl.NumberFormat将其格式化为货币。
function formatProdPrice(value) {
return new Intl.NumberFormat("en-US", { style: "currency", currency: "USD" }).format(value);
}
formatProdPrice(1000.55); // returns "$1,000.55"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
49 次 |
| 最近记录: |