Exactly, floating-point division is the way to go! But if you need even more control over precision, you might want to round the result to avoid floating-point inaccuracies in certain cases. Java’s Math.round()
can help:
public static void main(String[] args) {
double g = Math.round((1.0 / 3) * 100.0) / 100.0; // Rounds to two decimal places
System.out.printf("%.2f", g); // Output: 0.33
}
This ensures your result is rounded properly to two decimal places. Not always needed, but useful if you’re dealing with precise financial calculations or need to avoid floating-point quirks in java division.