Name: Anonymous 2008-02-14 8:16
I was wondering if any of you java geeks could help me with the following:
package testproject;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
doubleClass l = new doubleClass();
while(true)
{
l.countUp();
System.out.println(l.getNumber());
Scanner in = new Scanner(System.in);
// wait for <Enter> to be pressed
in.nextLine();
}
}
}
package testproject;
public class doubleClass {
private double number;
public doubleClass()
{
number = 0;
}
public void countUp()
{
number += 0.01;
}
public double getNumber()
{
return number;
}
}
So basically a very simple program to test a double.
The problem is that the number fucks up at some points.
e.g. instead of printing 0.06 it prints 0.060000000000000005
Why the fuck is this and how can I get it to work normally
like in c or c++?
package testproject;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
doubleClass l = new doubleClass();
while(true)
{
l.countUp();
System.out.println(l.getNumber());
Scanner in = new Scanner(System.in);
// wait for <Enter> to be pressed
in.nextLine();
}
}
}
package testproject;
public class doubleClass {
private double number;
public doubleClass()
{
number = 0;
}
public void countUp()
{
number += 0.01;
}
public double getNumber()
{
return number;
}
}
So basically a very simple program to test a double.
The problem is that the number fucks up at some points.
e.g. instead of printing 0.06 it prints 0.060000000000000005
Why the fuck is this and how can I get it to work normally
like in c or c++?