将Double转换为Int时的不同答案 - Java vs .Net

时间:2021-02-06 18:55:28

In C# if I want to convert a double (1.71472) to an int then I get the answer 2. If I do this in Java using intValue() method, I get 1 as the answer.

在C#中,如果我想将double(1.71472)转换为int,那么我得到答案2.如果我使用intValue()方法在Java中执行此操作,我得到1作为答案。

Does Java round down on conversions?

Java是否会对转换进行舍入?

Why do the Java API docs have such scant information about their classes i.e.

为什么Java API文档关于其类的信息很少,即

Returns the value of the specified number as an int. This may involve rounding or truncation.

以int形式返回指定数字的值。这可能涉及舍入或截断。

A bit more info about the rounding would have been helpful!

关于舍入的更多信息会有所帮助!

3 个解决方案

#1


13  

Java rounds toward zero when narrowing from a floating point to an integer type—and so does C#, when you use the casting conversion. It's Convert.ToInt32 that rounds:

当你从浮点缩小到整数类型时,Java会向零舍入 - 当你使用转换转换时,C#也是如此。这是转换的Convert.ToInt32:

double d = 1.71472;
int x = (int) d; // x = 1
int y = Convert.ToInt32(d); // y = 2

Details can be found in the Java Language Specification. Note that while the documentation cited in Number leaves options open for subclasses, the documentation on the concrete boxing types, like Double, is explicit about the implementation:

详细信息可以在Java语言规范中找到。请注意,虽然Number中引用的文档为子类打开了选项,但有关具体装箱类型的文档(如Double)是明确的实现:

Returns the value of this Double as an int (by casting to type int).

以int形式返回此Double的值(通过强制转换为int类型)。

When using BigDecimal, you can specify one of eight different rounding policies.

使用BigDecimal时,您可以指定八种不同的舍入策略之一。

#2


7  

Java rounds toward zero when narrowing from a floating point to an integer type.

当从浮点缩小到整数类型时,Java向零舍入。

There's more documentation about the general rules in the Java Language Specification. Note that while the documentation in Number leaves options open for subclasses, the documentation on the concrete boxing types is more explicit:

有关Java语言规范中的一般规则的更多文档。请注意,虽然Number中的文档为子类打开了选项,但有关具体装箱类型的文档更明确:

Returns the value of this Double as an int (by casting to type int).

以int形式返回此Double的值(通过强制转换为int类型)。

When using BigDecimal, you can specify one of eight different rounding policies.

使用BigDecimal时,您可以指定八种不同的舍入策略之一。

#3


3  

Jon Skeet is correct, but something else to watch for is that .NET uses Banker's Rounding as its rounding algorithm. When you're halfway between round towards the even integer.

Jon Skeet是正确的,但需要注意的是.NET使用Banker的Rounding作为舍入算法。当你在往中等整数的中间。

#1


13  

Java rounds toward zero when narrowing from a floating point to an integer type—and so does C#, when you use the casting conversion. It's Convert.ToInt32 that rounds:

当你从浮点缩小到整数类型时,Java会向零舍入 - 当你使用转换转换时,C#也是如此。这是转换的Convert.ToInt32:

double d = 1.71472;
int x = (int) d; // x = 1
int y = Convert.ToInt32(d); // y = 2

Details can be found in the Java Language Specification. Note that while the documentation cited in Number leaves options open for subclasses, the documentation on the concrete boxing types, like Double, is explicit about the implementation:

详细信息可以在Java语言规范中找到。请注意,虽然Number中引用的文档为子类打开了选项,但有关具体装箱类型的文档(如Double)是明确的实现:

Returns the value of this Double as an int (by casting to type int).

以int形式返回此Double的值(通过强制转换为int类型)。

When using BigDecimal, you can specify one of eight different rounding policies.

使用BigDecimal时,您可以指定八种不同的舍入策略之一。

#2


7  

Java rounds toward zero when narrowing from a floating point to an integer type.

当从浮点缩小到整数类型时,Java向零舍入。

There's more documentation about the general rules in the Java Language Specification. Note that while the documentation in Number leaves options open for subclasses, the documentation on the concrete boxing types is more explicit:

有关Java语言规范中的一般规则的更多文档。请注意,虽然Number中的文档为子类打开了选项,但有关具体装箱类型的文档更明确:

Returns the value of this Double as an int (by casting to type int).

以int形式返回此Double的值(通过强制转换为int类型)。

When using BigDecimal, you can specify one of eight different rounding policies.

使用BigDecimal时,您可以指定八种不同的舍入策略之一。

#3


3  

Jon Skeet is correct, but something else to watch for is that .NET uses Banker's Rounding as its rounding algorithm. When you're halfway between round towards the even integer.

Jon Skeet是正确的,但需要注意的是.NET使用Banker的Rounding作为舍入算法。当你在往中等整数的中间。