[英]Javascript How To Format An Integer As A Currency String?
I have an integer stored as US cents, ie 1700 cents.我有一个整数存储为美分,即 1700 美分。 How can I convert that to a string that is $17.00 using javascript?
如何使用 javascript 将其转换为 $17.00 的字符串? I have tried
toFixed
and Intl.NumberFormat
but they return $1700?我尝试过
toFixed
和Intl.NumberFormat
但它们返回 1700 美元?
You can use toLocaleString()
function for this.您可以为此使用
toLocaleString()
函数。
var cents = 1629; var dollars = cents / 100; dollars = dollars.toLocaleString("en-US", {style:"currency", currency:"USD"}); console.log(dollars);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.