Javascript 从变量中去除小数点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5628771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
strip decimal points from variable
提问by Zac
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 ...
好吧,我知道这可能真的很容易,但我正在挣扎。我有一系列带有小数点和几个零的变量。如何剥离变量使其从 1.000 变为 1 ?不要认为这很重要,但数字是从我用 jquery 抓取的 xml 文件生成的,例如......
var quantity = $("QTY",this).text();
Thanks in advance for any help!
在此先感谢您的帮助!
回答by mVChr
Simply...
简单地...
Math.round(quantity);
...assuming you want to round 1.7
to 2
. If not, use Math.floor
for 1.7
to 1
.
...假设你想四舍五入1.7
到2
. 如果没有,用Math.floor
的1.7
到1
。
回答by danniel
use parseInt();
用 parseInt();
parseInt("1.25");//returns 1
parseInt("1.85");//returns 1
parseInt(1.25);//returns 1
parseInt(1.85);//returns 1
回答by itsashis4u
Use number = ~~number
用 number = ~~number
This is the fastest substitute to Math.floor()
这是最快的替代品 Math.floor()
回答by Valentine
parseInt is the slowest method math.floor is the 2nd slowest method
parseInt 是最慢的方法 math.floor 是第二慢的方法
faster methods not listed here are:
此处未列出的更快方法是:
var myInt = 1.85 | 0; myInt = 1;
var myInt = 1.85 | 0; myInt = 1;
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
速度测试在这里完成:http: //jsperf.com/math-floor-vs-math-round-vs-parseint/2
回答by A Grand Jovian
Use Math.trunc()
. It does exactly what you ask. It strips the decimal.
使用Math.trunc()
. 它完全符合您的要求。它去除小数点。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/trunc
回答by KushalP
For rounding numbers to the nearest Integer you can use Math.round()
like so:
要将数字四舍五入到最接近的整数,您可以Math.round()
像这样使用:
Math.round("1.000"); // Will produce 1
Math.round(123.4234); // Will produce 123
回答by Khez
回答by Mystical
A faster, more efficient way would be to use JavaScript's bitwise operators, like so:
一种更快、更有效的方法是使用 JavaScript 的按位运算符,如下所示:
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 integerstripped of all decimal places.
的|
(或操作者)将处理的编号的32位(二进制0s
和1s
),然后通过返回取决于所希望的结果的操作数,在此情况下将导致一个整数剥离所有小数位。
回答by JuicY_Burrito
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.
我建议你使用一种叫做Math.trunc()
...的东西,把你的号码放在括号里。我不建议你使用的原因Math.round()
是它可能会改变你的十进制数的整数部分,有些人不想要,但Math.round()
如果你知道你想得到最接近的整数,你可以使用它。
回答by Mike Gledhill
Here's another nice example:
这是另一个很好的例子:
I often use Math.round
andtoLocateString
to convert numbers containing decimal places, into a more readable string, with thousand separators:
我经常使用Math.round
和toLocateString
将包含小数位的数字转换为更易读的字符串,并带有千位分隔符:
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).
我发现这在从 JSON Web 服务加载十进制值时很有用,并且需要在网页上以更友好的格式显示它们(当然,当我不需要显示所有小数位时)。