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.

Printable View

- 09-25-2010, 11:52 PMs0meb0dyHow 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.

- 09-25-2010, 11:57 PMFubarable
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. - 09-26-2010, 12:01 AMs0meb0dy
I looked but i don't completely understand what its saying. Ive tried it but it didn't work

- 09-26-2010, 12:06 AMNormQuote:

it didn't work

- 09-26-2010, 12:09 AMFubarable
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!

- 09-26-2010, 12:12 AMs0meb0dy
can i give you my source code and you can look at it and see my errors?

- 09-26-2010, 12:14 AMFubarable
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:

Code:`[code]`

// your code goes here

// notice how the top and bottom tags are different

[/code]

- 09-26-2010, 12:14 AMs0meb0dy
it said something like it cant find method, but i imported everything, i know that

- 09-26-2010, 12:17 AMFubarable
sent as a private message:

Quote:

Originally Posted by**s0meb0dy**

Again, though, please use code tags (please see above). - 09-26-2010, 12:21 AMs0meb0dyMy Source code(Please don't publish or share)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...");

}

}

}

- 09-26-2010, 12:21 AMs0meb0dyThe
Program is used to calculate Differential analysis

- 09-26-2010, 12:25 AMFubarable

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:

Code:`Answer = SciNoteP1.multiply(dBaseminusTarget);`

- 09-26-2010, 12:27 AMs0meb0dy
its a double d = double s= string i = int

i can only use double with the Math.pow(); method - 09-26-2010, 12:30 AMs0meb0dy
how do i convert it then

- 09-26-2010, 12:30 AMFubarable
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.,

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. - 09-26-2010, 12:31 AMs0meb0dy
convert dBaseminusTarget to a BigDecimal

- 09-26-2010, 12:32 AMs0meb0dy
but i cant use scanner with a BigDecimal

- 09-26-2010, 12:32 AMFubarable
- 09-26-2010, 12:33 AMs0meb0dy
this has nothing to do with beer

- 09-26-2010, 12:33 AMFubarable