Rev - 8 months ago 58

Javascript Question

I have the results of a division and I wish to discard the decimal portion of the resultant number.

How can I do this?

Answer

You could use `Math.floor()`

(round down), `Math.ceil()`

(round up) or `Math.round()`

(round to nearest integer), dependent on how you wanted to remove the decimal.

If you simply want to truncate the decimal portion, use a bitwise operator (.e.g `|0`

) which will treat its operand as a signed 32bit integer.

You may also be talking about the inaccuracy of decimal rounding with floating point arithmetic.

Required Reading - What Every Computer Scientist Should Know About Floating-Point Arithmetic.

Source (Stackoverflow)