在JavaScript中显示带有固定小数位置的整数

发布于 2025-02-08 11:29:36 字数 364 浏览 1 评论 0原文

我想构建一个函数,该功能可以采用任何数字并使用两个小数点位置返回。重要的是,我希望能够拥有两个0,例如150.00。

我尝试使用tofix(2),但它返回了在这种情况下不可接受的字符串。我尝试使用数字()转换tofixed()的输出,但是从我可以告诉的是将150.00转换回150。

const takeNumber = (numberToConvert) => Number((Math.round(numberToConvert * 100) / 100).toFixed(2))

takeNumber(150) // should return 150.00

我还能做什么?将其包裹在parsefloat中似乎也不起作用。

I want to build a function which can take any number and return it with two decimal places. Importantly, I want to be able to have two 0s, e.g. 150.00.

I have tried using toFixed(2) but it returns a string which is not acceptable in this instance. I have tried converting the output of toFixed() with Number() but from what I can tell this converts 150.00 back to 150.

const takeNumber = (numberToConvert) => Number((Math.round(numberToConvert * 100) / 100).toFixed(2))

takeNumber(150) // should return 150.00

What else can I do? Wrapping it in parseFloat doesn't seem to work either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文