How to limit decimal places in Java?

To limit decimal places in Java, use DecimalFormat.

Here's how you do it:

double value = 10000.12345;
String noDecimalPlaces = new java.text.DecimalFormat("0").format(value);
String oneDecimalPlace = new java.text.DecimalFormat("0.0").format(value);
String twoDecimalPlaces = new java.text.DecimalFormat("0.00").format(value);

System.out.println(noDecimalPlaces); // 10000
System.out.println(oneDecimalPlace); // 10000.1
System.out.println(twoDecimalPlaces); // 10000.12