简体   繁体   English

为什么int + BigDecimal不起作用?

[英]Why does int + BigDecimal not work?

I encountered this problem, which was that I couldn't add an integer to a BigDecimal. 我遇到了这个问题,即我无法将一个整数添加到BigDecimal中。 In the code, I looked at the error and it said "bad operand types for binary operator '+'". 在代码中,我查看了错误,并说“二进制运算符'+'的错误操作数类型”。 How do I add integer to BigDecimal? 如何将整数添加到BigDecimal? (Represented in code as 1 + sqf) (在代码中表示为1 + sqf)

BigDecimal sqf = new BigDecimal(Math.sqrt(5));
sqf.setScale(100);
BigDecimal bd = new BigDecimal((1 + sqf) / 2); //Error here (1 + sqf)
bd.setScale(100);
System.out.println(isBuzzNumber(77707));
System.out.println(findHypotenuse(9, 10));
System.out.println("Phi (φ) = " + bd);

BigDecimal bd = BigDecimal.ONE.add(sqf).divide(new BigDecimal(2))

标准运算符仅适用于原语。

You will have to cast in either of the formats. 您将必须使用任何一种格式进行转换。 BigDecimal is immutable. BigDecimal是不可变的。

For instance 例如

sqf = sqf.add(new BigDecimal(1));

You can add by constructing a new BigDecimal class. 您可以通过构造一个新的BigDecimal类来添加。

  1. Java doesn't have operator overloading, thus any standard operator applies to primitive type only Java没有运算符重载,因此任何标准运算符仅适用于原始类型
  2. BigDecimal isn't included in the autoboxing mechanism, because it doesn't have primitive type representation 自动装箱机制不包含BigDecimal,因为它没有原始类型表示形式
  3. Read up BigDecimal API , it implements mathematical operators as methods (which mostly requires another BigDecimal instance as operand. 读取BigDecimal API ,它将数学运算符实现为方法(通常需要另一个BigDecimal实例作为操作数。

Math signs (+-*/) are only available for String and primitive types in Java. 数学符号(+-* /)仅适用于Java中的String和基本类型
BigDecimal and BigInteger are not primitive types, so you can not use math signs directly with them. BigDecimal和BigInteger不是原始类型,因此您不能直接使用数学符号。
Consider using methods of instance instead when you want to do math calculate. 当您要进行数学计算时,请考虑使用实例方法。

You can create a BigDecimal out of an integer using the BigDecimal(int) constructor , as in new BigDecimal(1) or BigDecimal.ONE (thanks, Herp Derpington). 您可以使用BigDecimal(int)构造函数从整数中创建BigDecimal,就像在new BigDecimal(1)BigDecimal.ONE (感谢Herp Derpington)。

Like Integer and Double, `BigDecimal instances are immutable , which means that any one BigDecimal instance will never change value, and thus you can share or reuse instances that represent the same number . 像Integer和Double一样,`BigDecimal实例是不可变的 ,这意味着任何一个BigDecimal实例都不会更改值,因此您可以共享或重用表示相同数字的实例

Unlike Integer and Double, BigDecimal does not participate in autoboxing , which is the translation from int primitives to Integer object instances (and likewise across other types). 与Integer和Double不同,BigDecimal不参与自动装箱 ,这是从int原语到Integer对象实例的转换(以及其他类型的转换)。 In Java, operators like + and / apply only to primitives; 在Java中,像+/这样的运算符仅适用于原语。 though you can call sqf.add(BigDecimal.ONE) , there is no way to use + or call sqf.add(1) . 尽管您可以调用sqf.add(BigDecimal.ONE) ,但无法使用+或调用sqf.add(1)

To operate on BigDecimal instances, you'll need to constrain yourself to its methods (such as add and divide ), passing in BigDecimal instances (newly-created, if necessary). 要在BigDecimal实例上进行操作,您需要将自己限制在其方法(例如adddivide )上,并传入BigDecimal实例(如果需要,可以新创建)。 divide , in particular, offers a number of method overloads that allow you to choose the way that the number is rounded or scaled—which is one of its advantages over the built-in floating-point division; 尤其是, divide提供了许多方法重载 ,使您可以选择对数字进行舍入或缩放的方式,这是它相对于内置浮点除法的优势之一; if you encounter trouble, you may want to specify ROUND_TO_EVEN , which best matches the IEEE 754 behavior in the Java Language spec . 如果遇到麻烦,则可能需要指定ROUND_TO_EVEN ,它最匹配Java Language spec的IEEE 754行为

You can also use BigDecimal object's intvalue() function like this: 您还可以像这样使用BigDecimal对象的intvalue()函数:

BigDecimal bd = new BigDecimal((1 + sqf.intValue()) / 2); BigDecimal bd = new BigDecimal((1 + sqf.intValue())/ 2);

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

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