format decimals java code example
Example 1: java string format 2 decimal places
String strDouble = String.format("%.2f", 1.23456);
DecimalFormat df = new DecimalFormat("#.##");
String formatted = df.format(2.456345);
System.out.println(formatted);
Example 2: java double format
NumberFormat formatter = new DecimalFormat("#0.00");
System.out.println(formatter.format(4.0));
Example 3: java decimalformat
String pattern = "###,###.###";
DecimalFormat decimalFormat = new DecimalFormat(pattern);