double value = 4.2352989244d;
String.format("%.2f", value) //output "4.24");
String.format("%.3f", value)//output("4.235");
i will refer this site just read it and you will understand everything
i hope this help:https://zetcode.com/java/numberformat/
double r = 5.1234;
System.out.println(r); // r is 5.1234
int decimalPlaces = 2;
BigDecimal bd = new BigDecimal(r);
// setScale is immutable
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP);
r = bd.doubleValue();
System.out.println(r); // r is 5.12
Number Formatting