Page 1 of 2 12 LastLast
Results 1 to 20 of 34
  1. #1
    s0meb0dy is offline Member
    Join Date
    Sep 2010
    Posts
    42
    Rep Power
    0

    Default How do i Multiply a BigDecimal by a Double

    I'm trying to multiply a BigDecimal by a double but I can't figure out how. Can anyone help me out? I was thinking of just converting the double to a BigDecimal but don't know how either.

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

    Default

    Quote Originally Posted by s0meb0dy View Post
    I'm trying to multiply a BigDecimal by a double but I can't figure out how. Can anyone help me out? I was thinking of just converting the double to a BigDecimal but don't know how either.
    Have you had a look athte BigDecimal API? If not, please do so, and in fact that should be the first place you should look before even asking here as the solution is right there in the constructor section: BigDecimal(double val) constructor.

    Best of luck and HTH.

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

    Default

    I looked but i don't completely understand what its saying. Ive tried it but it didn't work

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    it didn't work
    Please explain and show the errors.

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

    Default

    Yep, yep. If you show what you've tried, we'll know what you are getting right and what you're getting wrong, and will be able to help you better. Cheers and much luck!

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

    Default

    can i give you my source code and you can look at it and see my errors?

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

    Default

    Quote Originally Posted by s0meb0dy View Post
    can i give you my source code and you can look at it and see my errors?
    Yep, that's the idea. Also, if you get any error messages, please post the actual messages and indicate which lines are causing them. If you do post code here though, please don't forget to use code tags so that your code retains its formatting and is readable.

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [code] above your pasted code and the tag [/code] below your pasted code like so:

    Java Code:
    [code]
      // your code goes here
      // notice how the top and bottom tags are different
    [/code]
    Luck!

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

    Default

    it said something like it cant find method, but i imported everything, i know that

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

    Default

    sent as a private message:
    Quote Originally Posted by s0meb0dy
    My Source code (please don't publish or share)
    Java Code:
     //... code went here
    Nope. You have to post any pertinent code in the forum, not as a private message. I suggest that when you work to try to solve a problem such as this, create a separate new program whose goal is to solve this current problem and do nothing else. Then if you don't solve it, you can post it here and we don't have gobs of code to go through and you don't have to worry about posting all your homework here.

    Again, though, please use code tags (please see above).

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

    Default My Source code(Please don't publish or share)

    Java Code:
    /**
     * @(#)DifAnalysis.java
     *
     * @https://sites.google.com/site/s0m3b0dysstuff/
     * @Ryan Fabela
     * @version 1.00 2010/9/24
     */
    
    import java.util.*;
    import java.io.*;
    import java.lang.Math;
    import java.math.BigDecimal;
    
    public class DifAnalysis {
    
       public static void main(String[] Arguments){
    
       	Scanner oSciNote = new Scanner(System.in);
    
    	String sSciNoteP1,sSciNoteP2,sTarget,sBase,sBaseminusTarget,sAnswer;
    
        BigDecimal SciNoteP1,Base,Target,SciNoteP2,BaseminusTarget,Answer;/* P1 Means out of 3.125 x 10^5 the 3.125 part.
        P2 means the 10^5 part. target is the target exponent, for example when trying to get to kilo- you would put 10^3. base is the metric prefixes exponetial value.
        answer should be self explanatory.*/
    
        double dTen1 = 10,dTen2 = 10,dTen3 = 10;
    
        String oMetricPrefixes = "tera-, T, 10^12\ngiga-, G, 10^9\nmega-, M, 10^6\nkilo-, k, 10^3\ndeci-, d, 10^-1\ncenti-, c, 10^-2\nmilli-, m,10^-3\nmicro-, , 10^-6\nnano-, n, 10^-9\npico-, p, 10^-12\nFor base units use 10^0";
    
    	System.out.println(oMetricPrefixes);
    
    
    	String oErrorMes;
    
        System.out.println();
    
        System.out.println("Instruction:\nWhen it asks you for the digit term use the first set of numbers, for example in 3.125 x 10^5 you would put 3.125.\nWhen it asks for the exponential term put the last set of numbers, for  exapmle you would put 5.\nWhen it asks for the base exponential value you would put the metric scientific notaion of the metric prefix, for example, when going from micrograms to kilograms you would put -6(refer to above chart)\nWhen it asks for the target exponential value you would use one of the above metric scientific notations, for example when trying to get to kilo- you would put 3./nMakesure to only put numbers and decimal points.  No commas!");
    
    	System.out.println();
    
    
        System.out.println("Please put enter the digit term:");
        sSciNoteP1 = oSciNote.nextLine();
    
    
    	System.out.println("Please enter the exponential term:");
    	double  dSciNoteP2 = oSciNote.nextDouble();
    	dTen1 = Math.pow(dTen1,dSciNoteP2);
    
    	System.out.println("Please enter the base exponential value:");
    	double dBase = oSciNote.nextDouble();
    	dTen2 = Math.pow(dTen2,dBase);
    	double dBase1 = dTen2;
    
    	System.out.println("Please enter the target exponential value:");
    	double dTarget = oSciNote.nextDouble();
    	dTen3 = Math.pow(dTen3,dTarget);
    	double dTarget1 = dTen3;
    
    	double dTen4 = 10;
    	double dBaseminusTarget = Math.abs(dTarget1 - dTen2);
    	dBaseminusTarget = Math.pow(dTen4, dBaseminusTarget);
    
    	if(dBase > dTarget){
    		Answer = SciNoteP1.multiply(dBaseminusTarget);
    		System.out.println(answer);
    	}
    	if(dBase < dTarget){
    		answer = SciNoteP1 / dBaseminusTarget;
    		System.out.println("The answer is: "+answer);
    	}
    	else{
            System.out.println("You Messed Up...");
    	}
    
      }
    
    
    }
    this is the source code its not done but yeah
    Last edited by Fubarable; 09-26-2010 at 01:22 AM. Reason: moderate edit: quote tags changed to code tags

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

    Default The

    Program is used to calculate Differential analysis

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

    Default

    Quote Originally Posted by s0meb0dy View Post
    Java Code:
    /**
     * @(#)DifAnalysis.java
     *
     * @https://sites.google.com/site/s0m3b0dysstuff/
     * @Ryan Fabela
     * @version 1.00 2010/9/24
     */
    
    import java.util.*;
    import java.io.*;
    import java.lang.Math;
    import java.math.BigDecimal;
    
    public class DifAnalysis {
    
       public static void main(String[] Arguments){
    
       	Scanner oSciNote = new Scanner(System.in);
    
    	String sSciNoteP1,sSciNoteP2,sTarget,sBase,sBaseminusTarget,sAnswer;
    
        BigDecimal SciNoteP1,Base,Target,SciNoteP2,BaseminusTarget,Answer;/* P1 Means out of 3.125 x 10^5 the 3.125 part.
        P2 means the 10^5 part. target is the target exponent, for example when trying to get to kilo- you would put 10^3. base is the metric prefixes exponetial value.
        answer should be self explanatory.*/
    
        double dTen1 = 10,dTen2 = 10,dTen3 = 10;
    
        String oMetricPrefixes = "tera-, T, 10^12\ngiga-, G, 10^9\nmega-, M, 10^6\nkilo-, k, 10^3\ndeci-, d, 10^-1\ncenti-, c, 10^-2\nmilli-, m,10^-3\nmicro-, , 10^-6\nnano-, n, 10^-9\npico-, p, 10^-12\nFor base units use 10^0";
    
    	System.out.println(oMetricPrefixes);
    
    
    	String oErrorMes;
    
        System.out.println();
    
        System.out.println("Instruction:\nWhen it asks you for the digit term use the first set of numbers, for example in 3.125 x 10^5 you would put 3.125.\nWhen it asks for the exponential term put the last set of numbers, for  exapmle you would put 5.\nWhen it asks for the base exponential value you would put the metric scientific notaion of the metric prefix, for example, when going from micrograms to kilograms you would put -6(refer to above chart)\nWhen it asks for the target exponential value you would use one of the above metric scientific notations, for example when trying to get to kilo- you would put 3./nMakesure to only put numbers and decimal points.  No commas!");
    
    	System.out.println();
    
    
        System.out.println("Please put enter the digit term:");
        sSciNoteP1 = oSciNote.nextLine();
    
    
    	System.out.println("Please enter the exponential term:");
    	double  dSciNoteP2 = oSciNote.nextDouble();
    	dTen1 = Math.pow(dTen1,dSciNoteP2);
    
    	System.out.println("Please enter the base exponential value:");
    	double dBase = oSciNote.nextDouble();
    	dTen2 = Math.pow(dTen2,dBase);
    	double dBase1 = dTen2;
    
    	System.out.println("Please enter the target exponential value:");
    	double dTarget = oSciNote.nextDouble();
    	dTen3 = Math.pow(dTen3,dTarget);
    	double dTarget1 = dTen3;
    
    	double dTen4 = 10;
    	double dBaseminusTarget = Math.abs(dTarget1 - dTen2);
    	dBaseminusTarget = Math.pow(dTen4, dBaseminusTarget);
    
    	if(dBase > dTarget){
    		Answer = SciNoteP1.multiply(dBaseminusTarget);
    		System.out.println(answer);
    	}
    	if(dBase < dTarget){
    		answer = SciNoteP1 / dBaseminusTarget;
    		System.out.println("The answer is: "+answer);
    	}
    	else{
            System.out.println("You Messed Up...");
    	}
    
      }
    
    
    }
    this is the source code its not done but yeah

    Again, please use code tags, not quote tags. I've edited your post and have made the changes.

    So, do you in fact convert your double to a BigDecimal using the appropriate BigDecimal constructor before multiplying it to another BigDecimal?

    For instance here:
    Java Code:
    Answer = SciNoteP1.multiply(dBaseminusTarget);
    is dBaseminusTarget a BigDecimal object or a double? It should be a BigDecimal object, and can easily be converted using the constructor I linked to above.

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

    Default

    its a double d = double s= string i = int
    i can only use double with the Math.pow(); method

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

    Default

    how do i convert it then

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

    Default

    Quote Originally Posted by s0meb0dy View Post
    its a double d = double s= string i = int
    i can only use double with the Math.pow(); method
    What does this have to do with the price of beer?

    I have to wonder whether you should be using double when using BigDecimal in the first place. My own impression is that even if you fix your compile error by using the appropriate constructor, e.g.,
    Java Code:
          BigDecimal bigDecimalA = new BigDecimal("4");
          
          double myDouble = 8.0;
          
          BigDecimal result;
          
          
          result = bigDecimalA.multiply(myDouble);  // no conversion so doesn't compile
          result = bigDecimalA.multiply(new BigDecimal(myDouble));  // conversion, so compiles


    You'll still lower the accuracy of the result to the double's range with the use of a double. Rather, all numbers should perhaps begin with and stay BigDecimals here. Note that BigDecimal has it's own power method that you can use.

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

    Default

    convert dBaseminusTarget to a BigDecimal

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

    Default

    but i cant use scanner with a BigDecimal

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

    Default

    Quote Originally Posted by s0meb0dy View Post
    how do i convert it then
    Did you check the API link that I posted in my first post in this thread?

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

    Default

    this has nothing to do with beer

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

    Default

    Quote Originally Posted by s0meb0dy View Post
    but i cant use scanner with a BigDecimal
    Why not? Scanner can get a String and BigDecimal can construct with a String? You really really really need to look at the BigDecimal API. I cannot stress this enough.

Page 1 of 2 12 LastLast

Similar Threads

  1. matrix multiply
    By slixtrix in forum New To Java
    Replies: 8
    Last Post: 09-13-2010, 07:50 AM
  2. multiply two matrixes
    By smart princess in forum New To Java
    Replies: 8
    Last Post: 12-06-2009, 07:43 PM
  3. BigDecimal question
    By orchid in forum New To Java
    Replies: 2
    Last Post: 08-12-2008, 02:44 PM
  4. convert a really big string (len 39) to bigdecimal
    By coolsig in forum Advanced Java
    Replies: 6
    Last Post: 06-14-2008, 03:48 AM
  5. BigDecimal - Help formulating if statments
    By shinjitsunohana in forum New To Java
    Replies: 5
    Last Post: 04-28-2008, 11:12 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
  •