How to round number in javascript if number has 0.04500001 as decimals?

Viewed 30

I came across a number like this in a project :

Number

I was wondering whats the case for rounding up this kind of number in real world project and how to round the number when it has lots of zeros like this :

 {item.size - item.filled}

I also tried toFixed but it applies it to all generated numbers so other numbers which aren't in the format like this one , will also get zeros after decimals.

{(item.size - item.filled).toFixed(7)}

How can I round this kind of number only If it's like this and what would be the normal scenario in a real world project ? Should we just not display all the zeros in decimals ?

1 Answers

I am always displaying two decimals, your number is a Javascript problem. I use this function to round it:

 export const roundNumberToTwoDecimals = (number) =>
    Math.round(number * 100 + Number.EPSILON) / 100;
Related