I have a series of variables that have a decimal point and a few zeros. How do I strip the variable so it goes from 1.000 to 1?
10 Answers
Simply...
Math.round(quantity);
...assuming you want to round 1.7
to 2
. If not, use Math.floor
for 1.7
to 1
.

- 49,587
- 11
- 107
- 104
use parseInt();
parseInt("1.25");//returns 1
parseInt("1.85");//returns 1
parseInt(1.25);//returns 1
parseInt(1.85);//returns 1

- 1,751
- 1
- 11
- 13
-
Best solution if you don't know how many decimal places the number has. – LuckyLuke Skywalker Aug 18 '22 at 13:00
Use number = ~~number
This is the fastest substitute to Math.floor()

- 389
- 1
- 3
- 16
-
I guess this is one of the best ways to strip decimal points from a number – Ashish Apr 25 '15 at 19:06
-
7Based on this test, I would say unless you are dedicated to people using your Safari browser... The trade off of "fastest" vs the obscurity of ~~ to debugging is not worth doing this. Math.floor() gives intent as well as performance. Its very important to make sure readability isn't completely tossed out in favor of minor performance gains, I would use this only in an extreme case of performance boosting. http://jsperf.com/tilde-vs-floor – Kris Boyd Jan 30 '17 at 15:30
-
-
For anyone wondering in 2022, it doesn't matter... as long as you're not using `parseInt`. https://jsben.ch/8H56M – jason. Jan 28 '22 at 06:06
Use Math.trunc()
. It does exactly what you ask. It strips the decimal.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc

- 375
- 5
- 15

- 151
- 1
- 2
parseInt is the slowest method math.floor is the 2nd slowest method
faster methods not listed here are:
var myInt = 1.85 | 0; myInt = 1;
var myInt = 1.85 >> 0; myInt = 1;
Speed tests done here: http://jsperf.com/math-floor-vs-math-round-vs-parseint/2

- 131
- 1
- 2
-
1For those curious about the `|` and `>>` operators, you can learn more on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators). Note: `The operands of all bitwise operators are converted to signed 32-bit integers in two's complement format.` – Jo. Jun 22 '17 at 17:52
For rounding numbers to the nearest Integer you can use Math.round()
like so:
Math.round("1.000"); // Will produce 1
Math.round(123.4234); // Will produce 123

- 10,976
- 6
- 34
- 27
Here's another nice example:
I often use Math.round
and toLocateString
to convert numbers containing decimal places, into a more readable string, with thousand separators:
var myNumberAsString = "1234567.890123" // "1234567.890123"
var myNumber = Math.round(0.0 + myNumberAsString); // 1234568
return myNumber.toLocaleString(); // "1,234,568"
I find this useful when loading decimal values from, say a JSON Web Service, and need to display them in a friendlier format on a web page (when, of course, I don't need to display all of the decimal places).

- 27,846
- 7
- 149
- 159
A faster, more efficient way would be to use JavaScript's bitwise operators, like so:
function stripDecimals(n) {
return n | 0;
}
// examples:
stripDecimals(23.245); // => 23
stripDecimals(100.015020); // => 100
The |
(OR operator) will treat the numbers as 32-bit (binary 0s
and 1s
), followed by returning the desired result depending on the operands, which in this case would result to an integer stripped of all decimal places.

- 2,505
- 2
- 24
- 43
I suggest you use something called Math.trunc()
... put your number in the parentheses. The reason I don't suggest you use Math.round()
is that it might change the integer part of your decimal number which some people won't want though you can use Math.round()
if you know you want to get the closest integer.