1

I have a project in which I must create a shopping cart with multiple options such as adding certain amounts of items to the cart. Everything is working correctly except when the user prompts for the total of the cart. It will output a crazy number e.g. $15.9614.9500000000000000013.0.

This is the code used to print out the total. All the values are doubles. Any help?

System.out.println("Your total comes to $" + 
                   (dynamicRope * dynamicRopeCost) + 
                   (staticRope * staticRopeCost) + 
                   (webbing * webbingCost));
Will Byrne
  • 681
  • 7
  • 15
Will
  • 11
  • 4
  • Can you post some value examples? – m0skit0 Mar 20 '15 at 00:21
  • 6
    Looks like those values are being concatenated as strings instead of added as numbers. Try adding parentheses, or use a separate `double` variable to store the total. – Mattias Buelens Mar 20 '15 at 00:24
  • the example given was using 4 feet of dynamic 5 feet of static and 6 feet of webbing – Will Mar 20 '15 at 00:28
  • just added a totalCost double. – Will Mar 20 '15 at 00:31
  • totalCost = ((dynamicRope * dynamicRopeCost) + (staticRope * staticRopeCost) + (webbing * webbingCost)); System.out.println("Your total comes to $" + totalCost); value is still a crazy decimal but with only one decimal and is displaying the real cost – Will Mar 20 '15 at 00:32
  • @Will `totalCost = ((dynamicRope * dynamicRopeCost) + (staticRope * staticRopeCost) + (webbing * webbingCost)); System.out.println("Your total comes to $" + totalCost);` is correct, you just have to format the value. Take a look at [`String.format( )`](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#format-java.lang.String-java.lang.Object...-). – neilvillareal Mar 20 '15 at 00:36
  • Im not sure what that is. I am halfway through my first semester in intro – Will Mar 20 '15 at 00:40
  • http://docs.oracle.com/javase/tutorial/i18n/format/numberFormat.html will give you something to go on - help you render a double as a currency. You're nearly there. String.format (look in the Javadocs) is another option. – Ashley Frieze Mar 20 '15 at 01:03
  • 2
    Don't use floating-point for currency. Use a `BigDecimal`. – user207421 Mar 20 '15 at 01:11
  • 2
    Use always for currency BigDecimal. http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Daniel Stancu Mar 20 '15 at 01:53
  • Yeah and make a Money class. – Rob Mar 20 '15 at 03:33
  • 1
    http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate http://stackoverflow.com/questions/588004/is-floating-point-math-broken?rq=1 when you have time, read this Oracle document [What Every Computer Scientist Should Know About Floating-Point Arithmetic](https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html) – phuclv Mar 20 '15 at 04:29

1 Answers1

1

Since you are dealing with values in a shopping cart and double's are only precise up to a point, I would recommend that you use BigDecimal's instead because they provide an exact representation of a number. The imprecision of doubles is what's causing your incorrect console output.

import java.math.BigDecimal;

BigDecimal dynamicRope = new BigDecimal("4"); // example value of 4
BigDecimal dynamicRopeCost = new BigDecimal("5.50"); // example value of 5.50

// Initialize the other variables as BigDecimal's here

System.out.println("Your total comes to $" +
                    dynamicRope.multiply(dynamicRopeCost)
                    .add(staticRope.multiply(staticRopeCost))
                    .add(webbing.multiply(webbingCost)));
Will Byrne
  • 681
  • 7
  • 15