Results 1 to 5 of 5
  1. #1
    s0meb0dy is offline Member
    Join Date
    Sep 2010
    Posts
    42
    Rep Power
    0

    Default 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:
    Java 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, the API will show you the way. If you look here: BigDecimal multiply method API entry

    you will see this:

    Java 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.,
    Java 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!

  3. #3
    s0meb0dy is offline Member
    Join Date
    Sep 2010
    Posts
    42
    Rep Power
    0

    Default

    Thank you ill try it real quick

  4. #4
    s0meb0dy is offline Member
    Join Date
    Sep 2010
    Posts
    42
    Rep Power
    0

    Default

    thank you it worked in the above program ill try it in my Major one now to see if it works thanks alot!

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Adding BigDecimals
    By ShoeNinja in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 12:19 PM
  2. My buttons all perform their action like 100 times
    By 711groove in forum New To Java
    Replies: 0
    Last Post: 12-13-2009, 10:49 AM
  3. Cannot perform a PING
    By mnementh64 in forum Networking
    Replies: 1
    Last Post: 09-17-2009, 04:39 PM
  4. Perform one action at a time
    By Melki in forum AWT / Swing
    Replies: 6
    Last Post: 12-08-2008, 07:29 AM
  5. How to perform some event to button click
    By eva in forum AWT / Swing
    Replies: 2
    Last Post: 01-16-2008, 12:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •