troubleshooting Question

Multiplication accurary

Avatar of infinidem
infinidemFlag for United States of America asked on
Java
6 Comments2 Solutions238 ViewsLast Modified:
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 );

With the following output:

0.90000000000000000832667268468867405317723751068115234375
0.8999999999999999
0.8999999999999999
0.8999999999999999
0.9
0.9

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros