9

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.

Michael Treat
  • 359
  • 1
  • 4
  • 10
  • 1
    I don't understand what the problem is with the first code example. – robertklep Aug 28 '17 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 Aug 28 '17 at 11:38
  • If you just want to display the digits then you could turn the number into a string and add the zeros after the last decimal place. – alex Aug 28 '17 at 11:46
  • 1
    HTML can't even display numbers, they are converted to strings anyway ... – Teemu Aug 28 '17 at 11:47
  • I have to output a number to precisely 6 decimal places. Most numbers I get will are longer that 6 places so truncating or rounding works, but for numbers less than 6 places I have no solution. – Michael Treat Aug 29 '17 at 00:52
  • 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 Aug 29 '17 at 00:59
  • 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 May 03 '19 at 13:35

4 Answers4

6

Try this:

parseFloat(val.toFixed(6))
Zoe
  • 27,060
  • 21
  • 118
  • 148
joe hoeller
  • 1,248
  • 12
  • 22
0

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

JohnPan
  • 1,185
  • 11
  • 21
  • This returns item 3 in OP's list. – Teemu Aug 28 '17 at 11:43
  • it returns "0.500000" which, from what I understand is what he wants... Not so clear though... – JohnPan Aug 28 '17 at 18:21
  • Correct Teemu, I need to output a number to 6 decimals. toFixed returns a string, and when I try to convert it to a number it will drop off the extra 0's and give me 0.5 as the number. Type coercion doesn't work either as the function it is being outputted to will check type and I have no control of that function. – Michael Treat Aug 29 '17 at 00:46
  • Still, I cannot see why... If you need a Number, you need it to calculate things, and .5 IS equal to .50000000 so no problem... If you want to DISPLAY a Number, why care if it will be a string? @Michael__Treat – JohnPan Aug 29 '17 at 10:21
  • The function I am outputting to is requiring precision to exactly 6 decimals and it will handle calculations for an algorithm on that end. – Michael Treat Aug 29 '17 at 16:18
0

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

l.g.karolos
  • 1,131
  • 1
  • 10
  • 25
  • The HTML is always text, hence this basically is item 1 in OP's list. – Teemu Aug 28 '17 at 11:44
  • That's good, but I'm outputting to another function that needs it to be a number. While it shows up here as 0.500000, if you actually check its type it is typeof string. I need to output a Number to 6 decimals. When you try to turn the string of "0.500000" to a number you will just get 0.5 – Michael Treat Aug 29 '17 at 01:04
0

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?

bennygenel
  • 23,896
  • 6
  • 65
  • 78
  • I have to output a number to another function that uses it, and that function is required to take in a number that is at least 6 decimal places long. I have no control over the function i'm outputting to, so I have to output a number exactly to 6 decimals long. Most numbers will be above 6 decimals in which case I can truncate or round, but for numbers less than 6 decimal places or numbers that get rounded to less that 6 decimals are what is giving me an issue. I think your solution my be the best shot atm though. – Michael Treat Aug 29 '17 at 01:03
  • Where is this function? If that is a javascript function I don't understand why there is a control for decimal places. If its not how are you triggering the function? – bennygenel Aug 29 '17 at 06:21