This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Decimal Place Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Decimal Place Problem" Watch "Decimal Place Problem" New topic
Author

Decimal Place Problem

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys, I can't get the right decimal format to print on my screen. Here's an example.

Zoom Percentage: 9/128 = 0.0
Zoom Percentage: 7/26 = 0.0
Zoom Percentage: 5/26 = 0.0
Zoom Percentage: 7/19 = 0.0
Zoom Percentage: 2/19 = 0.0
Zoom Percentage: 3/18 = 0.0
Zoom Percentage: 2/18 = 0.0
Zoom Percentage: 1/14 = 0.0
Zoom Percentage: 2/14 = 0.0
Zoom Percentage: 1/12 = 0.0
Zoom Percentage: 0/12 = 0.0
Zoom Percentage: 0/14 = 0.0
Zoom Percentage: 5/14 = 0.0
Zoom Percentage: 3/13 = 0.0
Zoom Percentage: 0/13 = 0.0
Zoom Percentage: 0/9 = 0.0
Zoom Percentage: 0/9 = 0.0
Zoom Percentage: 0/10 = 0.0
Zoom Percentage: 1/10 = 0.0
Zoom Percentage: 1/5 = 0.0

The quotient always print as 0.0 where something like 9/128 should equal 0.0703125. Why is it? Here's the method where I do this stuff...



Thank you for your kind answers...


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21



In this line, you are doing an integer division, not a floating point division (even though zoomPercentage is a float). The result of the integer division is 0 and that's converted to a float.

Convert at least one of the numbers in the division to a float:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi! Thanks! The solution worked perfectly!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Decimal Place Problem