如何将一个数字转换为三位小数点?

huangapple go评论175阅读模式
英文:

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);

huangapple
  • 本文由 发表于 2023年6月8日 00:26:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定