Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you treat a Real data type as a float

 
Vadim Konkov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vadim Konkov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50251
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50251
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
He wasn't called "VadimK" at 8.00 o'clock.
 
Vadim Konkov
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem resolved. Thanks Campbell and Jesper. Please close thread.
[ October 14, 2008: Message edited by: Vadim Konkov ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50251
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic