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:
public static final double factor = 67/300d;
The problem is that 67
and 300
are integer literals, so the division ends up being an integer, which is 0. The d
at the end of the number makes it a double literal, so the result of 67/300d
is a double
.
Note that in the previous code the double literal is 300d
. You can also use 67d/300
or 67d/300d
.