Rev Rev - 1 year ago 95
Javascript Question

How can I remove the decimal part from JavaScript number

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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download