programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Simple Arithmetic Problem

Jerri Loh
Ranch Hand
Posts: 31
hi there, wanna check if i am coding this wrongly:

Shouldn't the answer, SimPercent, be 24.4 How come I am getting 0.0

Tks!

Hunter McMillen
Ranch Hand
Posts: 492
integer division truncates everything after the decimal point, so if you had an expression that was supposed to result in 0.244 (i.e. 23/43) you would only get the 0.
you should make sumOfVotes and howLong ints, or cast the expression to an int.

Hunter

Jerri Loh
Ranch Hand
Posts: 31
thanks hunter,

but yr suggestion doesn't work out.

But i got it solved.
Kramed from Daniweb:
The problem is you are doing integer division, which will truncate rather than round as you expect. In Java, 5/2 = 2 and 2/5 = 0 . To fix this problem, you need to make one of the operands in the division a floating point type (either float or double). Again, in Java, (float)5/2 = 2.5 and 2/(double)5 = 0.5 .

Hunter McMillen
Ranch Hand
Posts: 492
actually both of my suggestions work:

both print: 24.46808510638298

Hunter

Matthew Brown
Bartender
Posts: 4568
9