50

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?

Zac
  • 12,637
  • 21
  • 74
  • 122
  • possible duplicate of [Remove decimal from JavaScript number](http://stackoverflow.com/questions/7641818/remove-decimal-from-javascript-number) – Foreever Apr 24 '14 at 10:51

10 Answers10

85

Simply...

Math.round(quantity);

...assuming you want to round 1.7 to 2. If not, use Math.floor for 1.7 to 1.

mVChr
  • 49,587
  • 11
  • 107
  • 104
41

use parseInt();

parseInt("1.25");//returns 1
parseInt("1.85");//returns 1
parseInt(1.25);//returns 1
parseInt(1.85);//returns 1
danniel
  • 1,751
  • 1
  • 11
  • 13
20

Use number = ~~number

This is the fastest substitute to Math.floor()

Ashish
  • 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
  • 7
    Based 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
  • sometimes, JS is very cool with the syntax (#SugarCoatingForLife) – dod_basim Oct 02 '18 at 06:24
  • 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
15

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

Steve Schrab
  • 375
  • 5
  • 15
A Grand Jovian
  • 151
  • 1
  • 2
13

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

Valentine
  • 131
  • 1
  • 2
  • 1
    For 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
4

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
KushalP
  • 10,976
  • 6
  • 34
  • 27
1

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).

Mike Gledhill
  • 27,846
  • 7
  • 149
  • 159
1

You don't need jQuery for this.

You can use parseInt just fine. From the page:

document.write(parseInt("10.33") + "<br />"); // 10
Khez
  • 10,172
  • 2
  • 31
  • 51
0

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.

Mystical
  • 2,505
  • 2
  • 24
  • 43
0

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.