简体   繁体   English

如何在Java中使用double转换为int

[英]How does double to int cast work in Java

I am new to Java, and wondering how does double to int cast work ? 我是Java的新手,想知道如何对int转换工作? I understand that it's simple for long to int by taking the low 32 bits, but what about double (64 bits) to int (32 bits) ? 我理解通过采用低32位来实现long很简单,但是对于int(32位)来说,加倍(64位)呢? those 64 bits from double in binary is in Double-precision floating-point format (Mantissa), so how does it convert to int internally ? 来自二进制二进制的64位是双精度浮点格式(尾数),那么它如何在内部转换为int?

It's all documented in section 5.1.3 of the JLS. 这些都记录在JLS的5.1.3节中。

In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows: 在第一步中,浮点数转换为long(如果T为long)或转换为int(如果T为byte,short,char或int),如下所示:

If the floating-point number is NaN (§4.2.3), the result of the first step of the conversion is an int or long 0. 如果浮点数为NaN(§4.2.3),则转换的第一步结果为int或long 0。

  • Otherwise, if the floating-point number is not an infinity, the floating-point value is rounded to an integer value V, rounding toward zero using IEEE 754 round-toward-zero mode (§4.2.3). 否则,如果浮点数不是无穷大,则浮点值将四舍五入为整数值V,使用IEEE 754舍入为零的模式(第4.2.3节)向零舍入。 Then there are two cases: 然后有两种情况:

    • If T is long, and this integer value can be represented as a long, then the result of the first step is the long value V. 如果T很长,并且该整数值可以表示为long,则第一步的结果是长值V.

    • Otherwise, if this integer value can be represented as an int, then the result of the first step is the int value V. 否则,如果此整数值可以表示为int,则第一步的结果是int值V.

  • Otherwise, one of the following two cases must be true: 否则,以下两种情况之一必须为真:

    • The value must be too small (a negative value of large magnitude or negative infinity), and the result of the first step is the smallest representable value of type int or long. 该值必须太小(大幅度或负无穷大的负值),第一步的结果是int或long类型的最小可表示值。

    • The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long. 该值必须太大(大幅度或正无穷大的正值),第一步的结果是int或long类型的最大可表示值。

(The second step here is irrelevant, when T is int .) (当Tint时,这里的第二步是无关紧要的。)

In most cases I'd expect this to be implemented using hardware support - converting floating point numbers to integers is something which is usually handled by CPUs. 在大多数情况下,我希望使用硬件支持来实现 - 将浮点数转换为整数通常由CPU处理。

Java truncates its value if you use (int) cast, as you may notice: 如果您使用(int)强制转换,Java会截断其值,您可能会注意到:

    double d = 2.4d;
    int i = (int) d;
    System.out.println(i);
    d = 2.6;
    i = (int) d;
    System.out.println(i);

Output: 输出:

2
2

Unless you use Math.round, Math.ceil, Math.floor... 除非你使用Math.round,Math.ceil,Math.floor ......

You may want to read the Java specification: 您可能想要阅读Java规范:

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3 http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3

The relevant section is 5.1.3 - "Narrowing Primitive Conversion". 相关部分是5.1.3 - “缩小原始转换”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM