double Nag = 25;

Nag = Nag + Nag * (1/5);

System.out.println(Nag);

Why does Nag equal 25?????

Printable View

- 10-19-2010, 07:45 AMBlack_EyeArithmetic using doubles
double Nag = 25;

Nag = Nag + Nag * (1/5);

System.out.println(Nag);

Why does Nag equal 25????? - 10-19-2010, 07:54 AMsuresh.sa
- 10-19-2010, 07:57 AMmasijade
1/5 is integer math resulting in 0 as plain numbers in code (without decimals) are integers. to use doubles use 1/5.0 to "declare" one of the numbers a double and force double arithmetic rather than Integer arithmetic.

- 10-19-2010, 08:21 AMJosAH
... or simply write Nag/5 or if you want to make it look fancy: Nag*0.2

kind regards,

Jos - 10-19-2010, 08:35 AMBlack_Eye
thank you guys - I was having a lot of trouble with this problem for some time!