How do I perform basic math with BigDecimals

• 09-26-2010, 03:50 AM
s0meb0dy
How do I perform basic math with BigDecimals
i need to multiply, subtract, divide, get 10 to the power of a BigDecimal and the absloute value.

this is a sample with error on bottom:
Code:

```import java.util.*; import java.math.BigDecimal; public class tentothezero{         public static void main(String[]  arguments){         BigDecimal Bigy1 = new BigDecimal(36);         BigDecimal Bigy2 = new BigDecimal(11);         Bigy2 = new BigDecimal.multiply(Bigy1,Bigy2);         System.out.println(Bigy2); --------------------Configuration: <Default>-------------------- C:\Documents and Settings\Ryan\My Documents\tentothezero.java:10: cannot find symbol symbol  : class multiply location: class java.math.BigDecimal         Bigy2 = new BigDecimal.multiply(Bigy1,Bigy2);                               ^ 1 error Process completed.```
• 09-26-2010, 04:06 AM
Fubarable
Again, the API will show you the way. If you look here: BigDecimal multiply method API entry

you will see this:

Code:

```multiply public BigDecimal multiply(BigDecimal multiplicand)     Returns a BigDecimal whose value is (this × multiplicand), and whose scale is (this.scale() + multiplicand.scale()).     Parameters:         multiplicand - value to be multiplied by this BigDecimal.     Returns:         this * multiplicand```
In your attempt above, you're trying to call the method off of the class, and if you look at the method definition in the API, you'll notice that the method is not a static method unlike some other BigDecimal methods such as valueOf. This means that the method is not called off of the BigDecimal class, but off of a BigDecimal object.

You'll also notice that the method takes only one BigDecimal parameter, not two. So to call it correctly, call it on one BigDecimal object, say Bigy1, and pass the other BigDecimal object in as a parameter.

e.g.,
Code:

`Bigy2 = Bigy1.multiply(Bigy2);`
Also, you'll want to learn and use java naming conventions so that others (including us and your teachers) will understand your code better and quicker. Variables should begin with a lower-case letter and classes with an upper-case letter.

Best of luck and keep studying the API!
• 09-26-2010, 04:08 AM
s0meb0dy
Thank you ill try it real quick
• 09-26-2010, 04:09 AM
s0meb0dy
thank you it worked in the above program ill try it in my Major one now to see if it works thanks alot!
• 09-26-2010, 12:32 PM
Fubarable
You're welcome.