Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 237
Michael Treat Avatar asked Aug 28 '17 11:08

Michael Treat


People also ask

Does toFixed return string?

Description. In JavaScript, toFixed() is a Number method that is used to convert a number to fixed-point notation (rounding the result where necessary) and return its value as a string.

How do I return a number to toFixed?

The method toFixed(n) rounds the number to n digits after the point and returns a string representation of the result. We can convert it to a number using the unary plus or a Number() call, e.g write +num. toFixed(5) .

What is the use of toFixed() method in JavaScript?

The toFixed () method converts a number into a string, rounding to a specified number of decimals. Note: if the desired number of decimals are higher than the actual number, zeros are added to create the desired decimal length. Optional. The number of digits after the decimal point. Default is 0 (no digits after the decimal point)

How to round a number to a fixed value in JavaScript?

The JavaScript toFixed () method is used to round (formats) a number to a fixed notation point or convert the number into a string and its representation of a number with a specified number of decimal points. It returns value as a string.

Why does tostringfixed return a string?

Your answer is slightly misleading: toFixed is a formatting function, which has a sole purpose of converting a number to a string, formatting it using the specified number of decimals. The reason it returns a string is because it's supposed to return a string, and if it was named toStringFixed instead, OP wouldn't be surprised at the results.

How to get number with exact digits after decimal point in JavaScript?

The JavaScript number toFixed () method is used to get the string that represents a number with exact digits after a decimal point. n: It represents the number of digits after decimal point. String representation of the specified number with exact digits after a decimal point.


1 Answers

Try this:

parseFloat(val.toFixed(6))
like image 129
joe hoeller Avatar answered Oct 14 '22 05:10

joe hoeller