I have a config file which includes some factors I want to use for calculations.
public class Config {
public static final double factor = 67/300; // ~0,2233...
}
Im accessing the factors like this:
public class Calculate {
public static calc() {
...
result *= Config.factor;
...
When I do that Config.factor equals 0, so my result is 0, too. I don’t have that problem if I set the factor to 0.2233, but that wouldn’t be as accurate. Why doesn’t setting it to 67/300 work?
Try this:
The problem is that
67and300are integer literals, so the division ends up being an integer, which is 0. Thedat the end of the number makes it a double literal, so the result of67/300dis adouble.Note that in the previous code the double literal is
300d. You can also use67d/300or67d/300d.