3

I encountered this problem, which was that I couldn't add an integer to a 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? (Represented in code as 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);
TessellatingPi
  • 105
  • 1
  • 2
  • 6

6 Answers6

3

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

Standard operators apply to primitives only.

dpington
  • 1,844
  • 3
  • 17
  • 29
3
  1. Java doesn't have operator overloading, thus any standard operator applies to primitive type only
  2. BigDecimal isn't included in the autoboxing mechanism, because it doesn't have primitive type representation
  3. Read up BigDecimal API, it implements mathematical operators as methods (which mostly requires another BigDecimal instance as operand.
LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
1

You will have to cast in either of the formats. BigDecimal is immutable.

For instance

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

You can add by constructing a new BigDecimal class.

zikto
  • 11
  • 2
  • http://stackoverflow.com/questions/1846900/addition-for-bigdecimal shows a great example – zikto Sep 11 '14 at 02:41
1

Math signs (+-*/) are only available for String and primitive types in Java.
BigDecimal and BigInteger are not primitive types, so you can not use math signs directly with them.
Consider using methods of instance instead when you want to do math calculate.

Doug Hou
  • 550
  • 4
  • 14
0

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).

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.

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). In Java, operators like + and / apply only to primitives; though you can call sqf.add(BigDecimal.ONE), there is no way to use + or call 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). 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; if you encounter trouble, you may want to specify ROUND_TO_EVEN, which best matches the IEEE 754 behavior in the Java Language spec.

Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
0

You can also use BigDecimal object's intvalue() function like this:

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

DeepInJava
  • 1,871
  • 3
  • 16
  • 31