This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I remember from long ago, when I was in school, we had to write programs in the Pascal programming language. It had a data type for numbers with decimals called "real".
In Java, you have the data types "float" and "double" for decimal numbers. I guess that with that statement, your teacher means you should use the "float" datatype for numbers. Currently, I see you've used "double" in your source code.
Hey, thanks for the reply. You're right on that part, I changed the double to float and it works the same. But when I input 2.1 into my test driver, it outputs "The weight of your parcel is 2.0999999046325684 kilograms and the total cost to ship is $8.0". Any idea why it converts down?
Welcome to JavaRanch Find out about the %f tag; if you write %.2f your $8.0 will come out as $8.00. Full details are in the Formatter class documentation, but that is far too complicated to remember. There is a much simpler explanation in the Java Tutorials.
And 2.1 coming out as 2.09999999998745693749846570324784679872787309 happens all the time, particularly if you use a float (up to 7 significant figures) rather than a double (over 15 significant figures in favourable cases). We have a FAQ (look for no 20) with links to explanations about floating-point arithmetic. Every now and again you find something like 0.7 coming out as 0.700000000083746598749562389453246.
Actually, if you managed to print out all the numbers in a float or double which was imprecise it would end with a 5 because it is converting from decimal to binary and back.