gpt4 book ai didi

javascript - 在 JavaScript 中将整数美分转换为可读的美元金额?

转载 作者:数据小太阳 更新时间:2023-10-29 03:59:21 26 4
gpt4 key购买 nike

var num = 1629; // this represents $16.29
num.toLocaleString("en-US", {style:"currency", currency:"USD"});
// outputs $1,629

到目前为止,这是我能做到的最接近的。我尝试了 toLocaleString 提供的所有选项,但似乎没有简单的方法来获得我想要的结果(这不是预期的)。 JS中没有内置函数吗?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

最佳答案

尝试将美分数除以 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com