long类型的文字超出了范围

时间:2021-09-28 16:20:22

Hi I am trying to do some calculations for a unit converter im creating and have stumbled upon a problem.

你好,我正在为一个单元转换器做一些计算,我正在创建并发现一个问题。

out10 = doubleInput / 94605284000000000000000L;

Eclipse says that "The literal of type long is out of range", I didn't even think this was possible, but maybe some f you know how to work around it ?

Eclipse说“long类型的文字超出了范围”,我甚至不认为这是可能的,但是如果你知道怎么处理的话?

3 个解决方案

#1


3  

You could make it a double literal instead of a long literal, with some loss of accuracy. Assuming doubleInput is also a double, and the output is as well, then there's no reason not to do that. If you need a really big integer constant with perfect accuracy, use a bignum (google it).

你可以把它改成双字,而不是长字,这样就失去了一些准确性。假设双输入也是双输入,输出也是,那么没有理由不这么做。如果您需要一个非常大的整数常数,并且非常准确,请使用bignum(谷歌it)。

#2


4  

Type long cannot hold such a big value. I suggest you try type BigDecimal, which can hold values of any size.

类型长不能承载这么大的值。我建议您尝试使用BigDecimal类型,它可以保存任何大小的值。

new BigDecimal("94605284000000000000000") should work.

新BigDecimal(“94605284000000000000000”)。

#3


1  

In this case, there are basically two steps involved:

在这种情况下,基本上涉及两个步骤:

  • Parsing the literal to a valid value for that literal type (int in your example).
  • 将文本解析为该文本类型的有效值(在您的示例中为int)。
  • Converting that value to the target type.
  • 将该值转换为目标类型。

See the following expressions.

看到下面的表达式。

int z = (int) 2147483647; //Compiles.
int a = (int) 2147483648; //Doesn't compile, because the literal `2147483648` is outside the range of `int`.
int b = (int) 2147483648L;  //Compiles.

In your example, out10 = doubleInput / 94605284000000000000000L;, the compiler first assumes the literal 94605284000000000000000 as an int type which is outside the valid range of int (from -2,147,483,648 to 2147483647). Therefore, it issues a compiler error.

在您的示例中,out10 = doubleInput / 94605284000000000000000L;编译器首先假定文字94605284000000000000000作为int类型,该int类型在int的有效范围之外(从-2,147,483,648到2147483647)。因此,它会发出编译器错误。

#1


3  

You could make it a double literal instead of a long literal, with some loss of accuracy. Assuming doubleInput is also a double, and the output is as well, then there's no reason not to do that. If you need a really big integer constant with perfect accuracy, use a bignum (google it).

你可以把它改成双字,而不是长字,这样就失去了一些准确性。假设双输入也是双输入,输出也是,那么没有理由不这么做。如果您需要一个非常大的整数常数,并且非常准确,请使用bignum(谷歌it)。

#2


4  

Type long cannot hold such a big value. I suggest you try type BigDecimal, which can hold values of any size.

类型长不能承载这么大的值。我建议您尝试使用BigDecimal类型,它可以保存任何大小的值。

new BigDecimal("94605284000000000000000") should work.

新BigDecimal(“94605284000000000000000”)。

#3


1  

In this case, there are basically two steps involved:

在这种情况下,基本上涉及两个步骤:

  • Parsing the literal to a valid value for that literal type (int in your example).
  • 将文本解析为该文本类型的有效值(在您的示例中为int)。
  • Converting that value to the target type.
  • 将该值转换为目标类型。

See the following expressions.

看到下面的表达式。

int z = (int) 2147483647; //Compiles.
int a = (int) 2147483648; //Doesn't compile, because the literal `2147483648` is outside the range of `int`.
int b = (int) 2147483648L;  //Compiles.

In your example, out10 = doubleInput / 94605284000000000000000L;, the compiler first assumes the literal 94605284000000000000000 as an int type which is outside the valid range of int (from -2,147,483,648 to 2147483647). Therefore, it issues a compiler error.

在您的示例中,out10 = doubleInput / 94605284000000000000000L;编译器首先假定文字94605284000000000000000作为int类型,该int类型在int的有效范围之外(从-2,147,483,648到2147483647)。因此,它会发出编译器错误。