46

ok i know this is probably really easy but I am floundering.. 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 ? Dont think this is that important but the numbers are generated from an xml file that I am grabbing with jquery like ...

var quantity = $("QTY",this).text();

Thanks in advance for any help!

Kai - Kazuya Ito
  • 6,454
  • 5
  • 44
  • 50
Zac
  • 12,174
  • 21
  • 71
  • 117
  • 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

84

Simply...

Math.round(quantity);

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

mVChr
  • 48,301
  • 11
  • 105
  • 100
37

use parseInt();

parseInt("1.25");//returns 1
parseInt("1.85");//returns 1
parseInt(1.25);//returns 1
parseInt(1.85);//returns 1
danniel
  • 1,661
  • 11
  • 12
18

Use number = ~~number

This is the fastest substitute to Math.floor()

Ashish
  • 369
  • 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
  • 4
    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
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
11

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
  • 355
  • 4
  • 14
A Grand Jovian
  • 111
  • 1
  • 2
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,688
  • 6
  • 31
  • 27
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
  • 9,942
  • 2
  • 30
  • 51
0

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
  • 25,874
  • 6
  • 141
  • 151
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,066
  • 2
  • 20
  • 35
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.