Take the 2-minute tour ×

I have an weird issue. I'm trying to store the result of an equation into a double variable.

double s = (((100 + 1)*(1/3))/100 + (1/3));

This returns a value a 0 rather than .67 (the correct value calculated from a calculator). Any reason why this could happen?

Note: A solution of saying that I could just make s = .67 is not a solution,

Thanks in advance.

share|improve this question
If you add d after all your numbers, eg 100d, you'll use floating point operations, not integer ops. –  jedwards Mar 24 '13 at 14:21
As it is already answered, I am just adding a side-note that if you want precise value of 0.67 unlike something like 0.669999999999 you should use BigDecimal. stackoverflow.com/questions/322749/… –  gaganbm Mar 25 '13 at 13:57
add comment

2 Answers

up vote 8 down vote accepted

The following uses integer (i.e. truncating) division, the result of which is zero:


To get floating-point division, turn either of the argument into a double, e.g.


Thus, the overall expression becomes:

double s = (((100 + 1)*(1./3))/100 + (1./3));

1. is the same as 1.0. Other ways to express the same number as a double are 1d and 1D.

The above expression evaluates to 0.6699999999999999.

share|improve this answer
Yes, I don't know how I let this slip. Thanks so much. It's been bugging me for quite some time. –  Stephen D Mar 24 '13 at 14:21
add comment

The compiler sees your numers as int's..

try like this:

double s = (((100d + 1d)*(1d/3d))/100d + (1d/3d));

now the result will be:


share|improve this answer
add comment

Your Answer


