Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Decimal Place Problem

Timothy Sam
Ranch Hand
Posts: 751
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...

Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87

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:

Timothy Sam
Ranch Hand
Posts: 751
Hi! Thanks! The solution worked perfectly!

 Consider Paul's rocket mass heater.