Michael Treat
Michael Treat

Reputation: 509

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

Upvotes: 9

Views: 21089

Answers (4)

joe hoeller
joe hoeller

Reputation: 1277

Try this:

parseFloat(val.toFixed(6))

Upvotes: 7

l.g.karolos
l.g.karolos

Reputation: 1142

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

Upvotes: 0

bennygenel
bennygenel

Reputation: 24660

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?

Upvotes: 0

JohnPan
JohnPan

Reputation: 1210

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

Upvotes: 0

Related Questions