I want the help to produce the result of below calculation as 1.12 but the result is coming up 1.0
double k=(112)/100;
System.out.println(k);
Asked
Active
Viewed 48 times
0

Raman Sharma
- 73
- 7
-
use double k-=((double)112/100) – Ann Mar 22 '18 at 04:10
2 Answers
1
You are doing Integer division
causing it to lose the precision:
Replace
double k=(112)/100;
with
double k=(112.0)/100;

VHS
- 9,534
- 3
- 19
- 43
0
double k-=((double)112/100)
worked for me as suggested by agni
when we do division like (112/100) JVM gives int as an o/p, you are storing that 'int' in 'double' so JVM adds '.0' to the o/p causing loss of precision. so, here you have to tell the JVM to give o/p without any loss. for that reason we have to mention like `double k = (double)112/100; which is similar to "typecasting".

Raman Sharma
- 73
- 7