在JavaScript中显示带有固定小数位置的整数
我想构建一个函数,该功能可以采用任何数字并使用两个小数点位置返回。重要的是,我希望能够拥有两个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论