For a script I’m writing, I need display a number that has been rounded, but not the decimal or anything past it. I’ve gotten down to rounding it to the third place, but I’m not sure how to go about just dropping the decimal and everything past it, as it doesn’t seem like JavaScript has a substr function like PHP does.
Any recommendations?
If you have a string, parse it as an integer:
If you have a number, ECMAScript 6 offers
Math.truncfor completely consistent truncation, already available in Firefox 24+ and Edge:If you can’t rely on that and will always have a positive number, you can of course just use
Math.floor:And finally, if you have a number in [−2147483648, 2147483647], you can truncate to 32 bits using any bitwise operator.
| 0is common, and>>> 0can be used to obtain an unsigned 32-bit integer: