如何使用JavaScript将浮动数转换为固定的浮动号码
例如,我想计算3/6。 3/6的答案是0.5浮数。当我使用JavaScript Tofix(6)方法修复此数字时,它返回了“ 0.500000”。问题是“ 0.500000”是字符串。但是我想要一个浮点数,其中有6位(。)之后。我该怎么做?我尝试了这个,但它行不通。
parseFloat((3/6).toFixed(6))
//output = 0.5 but i want 0.500000
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,
如果您将其打印到控制台,它将删除不必要的零。当您需要打印多达5或10个小数点时,必须使用Tofixed方法将数字转换为字符串。
Well,
If you print it to the console it will remove the unnecessary zero. When you need to print up to 5 or 10 decimal points, you have to convert the number into a string using the toFixed method.
您为什么在
parsefloat()
内使用tofixed
,因为它正在返回您0.5
执行此操作
Why are you using
toFixed
inside theparseFloat()
because of that it is returning you0.5
Do this
我更新了答案。我不知道您想将输出用作数字。我认为您想实现的目标是不可能的。只有在将自己的价值作为字符串保持时,才有可能。
用于文档:
I updated my answer. I did not get that you wanted to use the output as a number. I don't think that what you want to achieve is possible. It's possible only if you keep you value as a string.
For documentation: https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed