How to round a number to N decimal places in Java?
Hi everyone! I'm new to Java and ran into a problem when working with fractional numbers. I need the number to be displayed with a certain number of decimal places after mathematical operations. For example, I'm calculating the arithmetic mean and want to show the result with two decimal places: 5.67, not 5.6748912.\n\nI tried using `System.out.printf("%.2f", number)`, but that's only for output. I need to save the number itself as rounded for further calculations. I tried doing this:\n\n```java\ndouble value = 5.6748912;\ndouble rounded = Math.round(value * 100) / 100;\nSystem.out.println(rounded); // Outputs 5.0, not 5.67!\n```\n\nWhy does this happen? I multiplied by 100, rounded, divided — it should work. Obviously, I'm missing something. I've also heard about `BigDecimal`, but I don't understand how to use it.