I am needing to truncate the number of decimals in a floating point number to a specific number and discarding the rest, at the moment the only method I found is toFixed
but this rounds the value not truncates:
let num = 21.777777;
console.log(num.toFixed(1));
console.log(num.toFixed(2));
console.log(num.toFixed(3));
console.log(num.toFixed(4));
It happened to someone else, how did you solve it, I was thinking of converting to a string and trimming the string, but I want to know if there is a way to do it without resorting to that.
EDITION
As mentioned, the code of the original answer has a bug when doing floating point operations, and that is that decimals are lost. We can solve it by seeing if the number of decimal places that the number has is less than or equal to the number of places that must be truncated.
Or following this idea simplify it like this:
I also did some tests which you can see here .
It's from the English site, but you might want to take a look at the answers to this question. This one is very interesting.
ORIGINAL ANSWER
With a bit of math:
There is no predefined function to do it and it's not trivial, because doing math with floating point numbers is always open to possible rounding errors, so it's best to play it safe: transform the number to a string and then stay with the desired decimals:
Explanation: The regular expression sticks with numbers until it finds a period. It then keeps the next two numbers it finds (or fewer, if none).
Note that the function returns a text, not a number. It can be transformed back to number using the function
Number(texto)
This feature is short, clear, concise and covers (hopefully) all (3 different) situations. For specific cases it can be simplified by simply eliminating what is not going to be used.
The calculation is performed in all three cases with small changes of the same statement. Cases:
In search of maximum simplicity and elegance, I prefer this new version:
x = Number(x); decimalDigits = Number(decimalDigits);
a. x = x * (10 ** decimalDigits); // move the decimal point to the right as many places as indicated by decimalDigits;
b. x = x - (x % 1); We calculate the decimal part of the new number (x % 1) and subtract it.
c. x = x / (10 ** decimalDigits); We move the decimal point to the initial position.
(This is the previous version that I leave in case someone prefers it:
Here parseInt was the one that was in charge of eliminating the decimal part)
returnx;
return x.toString();
d. toString() gives us the text representation of the same number.
Return text string INCLUDING non-significant zeros. Same as 2. but changing the last step of .toString() to: .toFixed(decimalDigits)
return x.toFixed(decimalDigits)
d. toFixed(decimaDigits) returns a text string including non-significant zeros if necessary so that it has exactly the number of decimal characters indicated by decimaDigits.
toFixed(decimalDigits), if there were any leftover decimals, it would also eliminate them -rounding instead of truncating- but in this case from the previous steps we know that none are left over: We have already truncated them in step b. (see preparation. Paragraph * 0.), at most non-significant zeros are missing.
You could also use the toPesition() function. What this function does is define the amount of numbers you want to be displayed, including decimals, in addition to that, it also approximates the numbers to a closer amount. Example:
I have an example that can help you, it shows you the result without rounding, it simply shows the result and the first two decimals:
I found a way with javascript.
the input formats it like this: 00000,00
I documented it in case someone needs to make an adaptation, it will be easier for them to locate where to do it.