'strip decimal points from variable
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!
Solution 1:[1]
use parseInt();
parseInt("1.25");//returns 1
parseInt("1.85");//returns 1
parseInt(1.25);//returns 1
parseInt(1.85);//returns 1
Solution 2:[2]
Use number = ~~number
This is the fastest substitute to Math.floor()
Solution 3:[3]
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
Solution 4:[4]
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
Solution 5:[5]
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
Solution 6:[6]
You don't need jQuery for this.
You can use parseInt just fine. From the page:
document.write(parseInt("10.33") + "<br />"); // 10
Solution 7:[7]
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).
Solution 8:[8]
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.
Solution 9:[9]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | danniel |
Solution 2 | Ashish |
Solution 3 | Valentine |
Solution 4 | Steve Schrab |
Solution 5 | KushalP |
Solution 6 | Khez |
Solution 7 | Mike Gledhill |
Solution 8 | |
Solution 9 |