This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How do you treat a Real data type as a float Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do you treat a Real data type as a float" Watch "How do you treat a Real data type as a float" New topic
Author

How do you treat a Real data type as a float

Vadim Konkov
Greenhorn

Joined: Sep 30, 2008
Posts: 8
I have an assignment for school in which I had to make a program that would calculate shipping fee's based on weight (in kilograms).

The problem now is that numbers are being rounded down, and the program is failing a JUnitTest...

java.lang.AssertionError: Parcel Weight: 0.20000000298023224
Expected: is <2.5>
got: <4.0>

I will post my code below if it helps..


Now the test exception:



And the driver:



Thanks for the time you dedicate to reading this over and hopefully trying to help me.
[ October 14, 2008: Message edited by: Vadim Konkov ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Vadim Konkov
Greenhorn

Joined: Sep 30, 2008
Posts: 8
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?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
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.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

"VadimK",
Welcome to the ranch. You may not be aware of the ranch Naming Policy. Please read it carefully and change your name accordingly (you need to set both first and last names). Thank you.


[My Blog]
All roads lead to JavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
He wasn't called "VadimK" at 8.00 o'clock.
Vadim Konkov
Greenhorn

Joined: Sep 30, 2008
Posts: 8
Problem resolved. Thanks Campbell and Jesper. Please close thread.
[ October 14, 2008: Message edited by: Vadim Konkov ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Thank you for correcting the name problem, and we are only too pleased to be able to help.

We only close threads if there is something wrong with them; old threads remain "open" for ever.

CR
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you treat a Real data type as a float