最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS- toFixed returns a string, but I need a number to 6 digits - Stack Overflow

programmeradmin4浏览0评论

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. 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.

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. 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.

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?

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.

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. 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.

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. 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.

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?

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.

Share Improve this question edited Sep 5, 2017 at 13:42 Michael Treat asked Aug 28, 2017 at 11:32 Michael TreatMichael Treat 5191 gold badge5 silver badges13 bronze badges 7
  • 1 I don't understand what the problem is with the first code example. – robertklep Commented Aug 28, 2017 at 11:36
  • 1 There's no such a number in JS. + you don't even need it. When showing, use the string representation, when calculating with the number, just use the native precision. – Teemu Commented Aug 28, 2017 at 11:38
  • 1 HTML can't even display numbers, they are converted to strings anyway ... – Teemu Commented Aug 28, 2017 at 11:47
  • 1 Numbers don't have a fixed amount of decimal places. They have a dynamic amount of binary places, and there's no way to change that. Sorry. If you care about decimal representation, use strings or get control over the output method. – Bergi Commented Aug 29, 2017 at 0:59
  • 1 Unlike all the stereotypical “snotty developer” answers above, I’ll answer your question: parseFloat(val.toFixed(6)); will return the number you desire. – joe hoeller Commented May 3, 2019 at 13:35
 |  Show 2 more ments

4 Answers 4

Reset to default 7

Try this:

parseFloat(val.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./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. 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. But have a look at this library it has plenty of stuff for you and it might help you

Decimal

Try Number( 1/2 ).toFixed(6) , it returns what you need

Basically 0.5 === 0.500000 if you think mathematically.

console.log((1 / 2) + (1 / 1000000)); // this would print out 0.500001 

toFixed() is intended to use for display purposes I think.

What do you want to achieve? Why can't use use it as string and use parseFloat() when you need to do some calculations?

发布评论

评论列表(0)

  1. 暂无评论