I
info
I know the following behaviour is an old problem,
but still I don't understand why such a simple piece of code:
double val = 0;
for(int i=0;i<10;i++) {
val+=0.1;
System.out.println(val);
}
has the following (terrible) output:
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
???
Why, as a developer, I have to use a trick like this to solve the
problem:
java.text.DecimalFormat df = new
java.text.DecimalFormat("###.########");
double val = 0;
for(int i=0;i<10;i++) {
val+=0.1;
val = df.parse(df.format(val)).doubleValue();
System.out.println(val);
}
Thank you in advance
Vincenzo Caselli
(e-mail address removed)
http://www.censnet.it
but still I don't understand why such a simple piece of code:
double val = 0;
for(int i=0;i<10;i++) {
val+=0.1;
System.out.println(val);
}
has the following (terrible) output:
0.1
0.2
0.30000000000000004
0.4
0.5
0.6
0.7
0.7999999999999999
0.8999999999999999
0.9999999999999999
???
Why, as a developer, I have to use a trick like this to solve the
problem:
java.text.DecimalFormat df = new
java.text.DecimalFormat("###.########");
double val = 0;
for(int i=0;i<10;i++) {
val+=0.1;
val = df.parse(df.format(val)).doubleValue();
System.out.println(val);
}
Thank you in advance
Vincenzo Caselli
(e-mail address removed)
http://www.censnet.it