Also keep in mind that whenever you work with money, you need to use a base10 type rather than a base2 since the latter is subject to classic IEEE-754 floating point rounding errors: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems
Java has the BigDecimal type for this purpose, though it's probably the most horrible support of a decimal type in any modern language. On May 10, 3:25 am, Luke Vandervort <[email protected]> wrote: > This should be simple for you guys. I am writing a simple program. The > correct answer is 630, however java comes up with 626.4. I assume this > is a rounding issue because I used the wrong variable type, but after > a couple hours on this thing I have no idea. Anyone have any > suggestions? > > public class yards > { > public static void main(String[] args) > { > byte length=25; > byte width=42; > int product= length * width; > float totalsquareyards=product / 9; > double cost= totalsquareyards * 5.4f; > > System.out.println(""); > System.out.print("The carpeting is " + length); > System.out.println(" feet long."); > System.out.println(""); > System.out.print("The carpeting is " + width); > System.out.println(" feet wide."); > System.out.println(""); > System.out.print("It will cost $" + cost); > System.out.println(" to carpet the room"); > System.out.println(""); > > } > } > > Thanks! > -- > Luke M. P. Vandervort > Linux, because rebooting is for adding new hardware ... > > -- > You received this message because you are subscribed to the Google Groups > "The Java Posse" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group > athttp://groups.google.com/group/javaposse?hl=en. -- You received this message because you are subscribed to the Google Groups "The Java Posse" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/javaposse?hl=en.
