var num = 1629; // this represents $16.29
num.toLocaleString("en-US", {style:"currency", currency:"USD"});
// outputs $1,629
到目前为止,这是我能做到的最接近的。我尝试了 toLocaleString 提供的所有选项,但似乎没有简单的方法来获得我想要的结果(这不是预期的)。 JS中没有内置函数吗?
最佳答案
尝试将美分数除以 100 得到等值的美元。即:
const number = 1629;
const dollars = (cents / 100).toLocaleString("en-US", {style:"currency", currency:"USD"});
dollars
现在等于“$16.29”
关于javascript - 在 JavaScript 中将整数美分转换为可读的美元金额?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32768494/