英文:
How I convert a number to 3 decimal points?
问题
I'm having a problem where I can't convert numbers to something like "0.037", given the case where I have 37 as one of these numbers, without this affecting larger numbers like "2015", which in turn manage to become "2.015".
I tried approaches like:
myNumber = parseFloat(myNumber.toFixed(3));
myNumber = (myNumber / 1000).toFixed(3);
I keep having my whole number the same "37" not "0.037".
The most efficient approach to the case so far has been parseFloat with toFixed, however, as stated, it only works on numbers that are larger, not integers like "0" and "37".
英文:
I'm having a problem where I can't convert numbers to something like "0.037", given the case where I have 37 as one of these numbers, without this affecting larger numbers like "2015", which in turn manage to become "2.015".
I tried approaches like:
myNumber = parseFloat(myNumber.toFixed(3));
myNumber = (myNumber / 1000).toFixed(3);
i keep having my whole number the same "37" not "0.037"
The most efficient approach to the case so far has been parseFloat with toFixed, however, as stated, it only works on numbers that are larger, not integers like "0" and "37".
答案1
得分: 1
这只是一个简单的语法错误,你需要像这样在JS中链接操作:
let myNumber = parseFloat(yourNumberHere).toFixed(3);
或者像这样:
let myNumber = parseFloat(yourNumber);
myNumber.toFixed(3);
英文:
its just a simple syntax error, you need to chain operations in JS like this:
let myNumber = parseFloat(yourNumberHere).toFixed(3);
or like this :
let myNumber = parseFloat(your number);
myNumber.toFixed(3);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论