Avatar of infinidem
infinidem
Flag for United States of America asked on

Multiplication accurary

Hi,

working on a simple problem and wanted to get some ideas of the best way around it.  Essentially I want to solve the following problem:

$0.75 * 120%

The answer is $0.90.

I have tried the following:

		BigDecimal bd1 = new BigDecimal(.75);
		BigDecimal bd2 = new BigDecimal(.2);
		BigDecimal bd3 = new BigDecimal(1);
		
		System.out.println( bd1.multiply(bd2.add(bd3)));
		
		System.out.println( 0.75 * ( 1 + 0.20) );

		System.out.println( .75d *  1.2d );
		
		System.out.println( .75d / 5d * 6d);
		
		System.out.println( 75d * 1.2d /100d);
		
		System.out.println( .75d * (( 1d + 0.2d) * 100d)/100d );

Open in new window


With the following output:

0.90000000000000000832667268468867405317723751068115234375
0.8999999999999999
0.8999999999999999
0.8999999999999999
0.9
0.9

Java

Avatar of undefined
Last Comment
infinidem

8/22/2022 - Mon
for_yan


Look at this recent question with solution:

https://www.experts-exchange.com/Programming/Languages/Java/Q_27240925.html?sfQueryTermInfo=1+10+30+bigdecim+yan

  BigDecimal federalTaxableEarnings =  new BigDecimal(40.0276);
 federalTaxableEarnings = federalTaxableEarnings.setScale(2, RoundingMode.HALF_UP);
        System.out.println("earning: " + federalTaxableEarnings);

Open in new window

for_yan

I think it should be like that for your code:

BigDecimal bd4 = bd1.multiply(bd2.add(bd3));
bd4 = bd4.setScale(2,RoundingMode.HALF_UP);
 System.out.println("bd4: " + bd4);

Let me test.
for_yan


Yes, this worked for me:
BigDecimal bd1 = new BigDecimal(.75);
		BigDecimal bd2 = new BigDecimal(.2);
		BigDecimal bd3 = new BigDecimal(1);

		
        BigDecimal bd4 = bd1.multiply(bd2.add(bd3));
        bd4 = bd4.setScale(2,RoundingMode.HALF_UP);
         System.out.println("bd4: " + bd4);
         

Open in new window


Output:
bd4: 0.90

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Frosty555

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
infinidem

ASKER
The answer is given to two parts as the explanation that a decimal can not be accurately represented via binary and that the constructors for BidDecimal should be with more accuracy.  Thanks Everyone.