Javascript 在Javascript中将double转换为int而不舍入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8388440/
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
Converting a double to an int in Javascript without rounding
提问by RSH1
In C# the following code returns 2:
在 C# 中,以下代码返回 2:
double d = 2.9;
int i = (int)d;
Debug.WriteLine(i);
In Javascript, however, the only way of converting a "double" to an "int" that I'm aware of is by using Math.round/floor/toFixed etc. Is there a way of converting to an int in Javascript without rounding? I'm aware of the performance implications of Number() so I'd rather avoid converting it to a string if at all possible.
然而,在 Javascript 中,我所知道的将“double”转换为“int”的唯一方法是使用 Math.round/floor/toFixed 等。 有没有办法在 Javascript 中转换为 int 而不舍入? 我知道 Number() 的性能影响,所以我宁愿尽可能避免将其转换为字符串。
回答by kubetz
Use parseInt()
.
使用parseInt()
.
var num = 2.9
console.log(parseInt(num, 10)); // 2
You can also use |
.
您也可以使用|
.
var num = 2.9
console.log(num | 0); // 2
回答by Pointy
I find the "parseInt" suggestions to be pretty curious, because "parseInt" operates on stringsby design. That's why its name has the word "parse" in it.
我发现“parseInt”的建议非常奇怪,因为“parseInt”是按照设计对字符串进行操作的。这就是为什么它的名字中有“解析”这个词的原因。
A trick that avoids a function call entirely is
一个完全避免函数调用的技巧是
var truncated = ~~number;
The double application of the "~" unary operator will leave you with a truncated version of a double-precision value. However, the value is limited to 32 bit precision, as with all the other JavaScript operations that implicitly involve considering numbers to be integers (like array indexing and the bitwise operators).
“~”一元运算符的双重应用将为您留下双精度值的截断版本。但是,该值仅限于 32 位精度,与所有其他隐式涉及将数字视为整数的 JavaScript 操作(如数组索引和按位运算符)一样。
edit— In an update quite a while later, another alternative to the ~~
trick is to bitwise-OR the value with zero:
编辑——在一段时间后的更新中,另一个替代~~
技巧是将值与零进行按位或:
var truncated = number|0;
回答by Justin Niessner
Just use parseInt()
and be sure to include the radix so you get predictable results:
只需使用parseInt()
并确保包含基数,以便获得可预测的结果:
parseInt(d, 10);
回答by Roland
Similar to C# casting to (int)
with just using standard lib:
(int)
与仅使用标准库的C# 转换类似:
Math.trunc(1.6) // 1
Math.trunc(-1.6) // -1
回答by hugomg
There is no such thing as an int
in Javascript. All Numbers
are actually doubles behind the scenes* so you can't rely on the type system to issue a rounding order for you as you can in C or C#.
int
在 Javascript 中没有像 an 这样的东西。所有Numbers
实际上都是幕后的双打*,因此您不能像在 C 或 C# 中那样依赖类型系统为您发出舍入顺序。
You don't need to worry about precision issues (since doubles correctly represent any integer up to 2^53) but you really are stuck with using Math.floor (or other equivalent tricks) if you want to round to the nearest integer.
您无需担心精度问题(因为双精度数可以正确表示最大为 2^53 的任何整数),但是如果您想四舍五入到最接近的整数,您确实会坚持使用 Math.floor(或其他等效技巧)。
*Most JS engines use native ints when they can but all in all JS numbers must still have double semantics.
*大多数 JS 引擎尽可能使用原生整数,但所有 JS 数字仍然必须具有双重语义。
回答by 1man
As @Quentin and @Pointy pointed out in their comments, it's not a good idea to use parseInt()
because it is designed to convert a string to an integer. When you pass a decimal number to it, it first converts the number to a string, then casts it to an integer. I suggest you use Math.trunc()
, Math.floor()
, ~~num
, ~~v
, num | 0
, num << 0
, or num >> 0
depending on your needs.
This performance testdemonstrates the difference in parseInt()
and Math.floor()
performance.
Also, this postexplains the difference between the proposed methods.
正如@Quentin 和@Pointy 在他们的评论中指出的那样,使用它不是一个好主意,parseInt()
因为它旨在将字符串转换为整数。当您将十进制数传递给它时,它首先将数字转换为字符串,然后将其转换为整数。我建议你使用Math.trunc()
,Math.floor()
,~~num
,~~v
,num | 0
,num << 0
,或num >> 0
根据您的需要。
这个性能测试演示的差异parseInt()
和Math.floor()
性能。此外,这篇文章解释了所提出方法之间的区别。