[英]JS- toFixed returns a string, but I need a number to 6 digits
I am working with another codebase that I have no control over, and that I cannot see. 我正在使用另一个我无法控制的代码库,而且我无法看到。 I can send it inputs and it will return outputs to me.
我可以发送输入,它会将输出返回给我。 This codebase also doesn't specify what it is written in, but that doesn't matter since I only need to pass it numbers and it hands me back it's outputted numbers.
这个代码库也没有指定它的内容,但这并不重要,因为我只需要传递它的数字,然后它将它的输出数字交给我。 I only know what the expected inputs and outputs are.
我只知道预期的输入和输出是什么。
One of the inputs I have to work with requires me to give an input that is exactly 6 decimal places long. 我必须使用的输入之一要求我提供一个长度恰好小数点后6位的输入。 Most of my numbers will be well above 6 decimal places, so I can just round or truncate it, but in the event that it is rounded to or randomly happens to be a number like
0.125
or 0.5
, I am not able to input the correct number. 我的大多数数字都会远远超过6位小数,所以我可以将它舍入或截断,但如果它舍入到或随机恰好是
0.125
或0.5
类的数字,我无法输入正确的数字数。
For instance, if I have a number like 0.5
, that is a valid and legitimate number, but the function I am inputting it to will not accept it and will err out because it is not a number to exactly 6 decimals places like 0.500000
is. 例如,如果我有一个像
0.5
的数字,这是一个有效和合法的数字,但我输入它的函数将不接受它并将错误,因为它不是一个数字到正好6 0.500000
数位,如0.500000
。 I also cannot input a string of "0.500000"
, as it will also err out since it is looking for a number, not a string. 我也不能输入一个
"0.500000"
的字符串,因为它也会出错,因为它正在寻找一个数字,而不是一个字符串。
Is there anyway in native JavaScript maintain a specific precision on a number, even if there are extra dead zeros on the end of it? 无论如何,本机JavaScript中的数字是否保持一个特定的精度,即使它的末尾有额外的死零?
Examples: 例子:
(1/2).toFixed(6) === "0.500000" // the toFixed() function returns a *String*, not a *Number*.
(1/2).toFixed(6) /1 === 0.5 // dividing by 1 coerces it to a Number, but drops the decimals back off the end.
Number( (1/2).toFixed(6) ) === 0.5 // Trying to convert it into a Number using the Number Constructor does the same as the example above it.
*EDIT: To be clear, I need a Number to 6 decimal places, not a String. *编辑:要清楚,我需要一个数字到小数点后6位,而不是一个字符串。
尝试Number( 1/2 ).toFixed(6)
,它返回你需要的东西
function myFunction() { var a = (1/2).toFixed(6) + "<br>"; var b = parseFloat(a).toFixed(6) + "<br>"; var x = new Decimal(123.4567) + "<br>"; var n = a + b + c; document.getElementById("demo").innerHTML = n; }
<script src="https://raw.githubusercontent.com/MikeMcl/decimal.js/master/decimal.min.js"> </script> <button onclick="myFunction()">Try it</button> <p id="demo"></p>
A number doesn't contain information about significant digits; 数字不包含有效数字的信息; the value 2 is the same as 2.0000000000000.
值2与2.0000000000000相同。 It's when you turn the rounded value into a string that you have make it display a certain number of digits.
当您将舍入值转换为字符串时,它会使其显示一定数量的数字。
It seems a little bit odd that a function requires a 6 decimal number. 一个函数需要一个6位十进制数似乎有点奇怪。 But have a look at this library it has plenty of stuff for you and it might help you
但看看这个库它有很多东西给你,它可能会帮助你
Basically 0.5 === 0.500000
if you think mathematically. 如果你在数学上思考,基本上
0.5 === 0.500000
。
console.log((1 / 2) + (1 / 1000000)); // this would print out 0.500001
toFixed() is intended to use for display purposes I think. 我认为toFixed()用于显示目的。
What do you want to achieve? 你想达到什么目的? Why can't use use it as string and use parseFloat() when you need to do some calculations?
为什么不能使用它作为字符串并在需要进行计算时使用parseFloat()?
尝试这个:
parseFloat(val.toFixed(6))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.