Well Hard to explain, but here it is. In java code, I use a double number,but this number get very small number at the end that I don't want. Maybe I need to use a better way to get the remainning.
Here's the code:
public class MyClass {
public static void main(String args[]) {
String vRetour = "";
Boolean valeurTrouve = false;
double chiffre = .08 ;
double valeurTravailDouble = 0;
long vIntegerPart = 0;
while (! valeurTrouve) {
for (int i = 0; i<101;i++) {
valeurTravailDouble = chiffre * 8;
vIntegerPart = (long) valeurTravailDouble;
vRetour = vRetour + vIntegerPart;
if ((valeurTravailDouble - vIntegerPart) == 0) {
valeurTrouve = true;
break;
}
chiffre = (valeurTravailDouble - vIntegerPart);
System.out.println(i + " = " + chiffre);
}
}
}
}
The problem is at the end, I have remaning undesired number like on line 1 , I have a 1 at the end. After on line 2 I have a 9. But this number increase and increase give me a bad at the line 17 because he increase to be part of my number.
- 0 = 0.64
- 1 = 0.1200000000000001
- 2 = 0.9600000000000009
- 3 = 0.6800000000000068
- 4 = 0.44000000000005457
- 5 = 0.5200000000004366
- 6 = 0.16000000000349246
- 7 = 0.2800000000279397
- 8 = 0.24000000022351742
- 9 = 0.9200000017881393
- 10 = 0.36000001430511475
- 11 = 0.880000114440918
- 12 = 0.04000091552734375
- 13 = 0.32000732421875
- 14 = 0.56005859375
- 15 = 0.48046875
- 16 = 0.84375
- 17 = 0.75
What's wrong, How I can deal with double without keeping the end of the number ?