I need to format (and not round off) a double to 2 decimal places.
I tried with:
String s1 = \"10.126\";
Double f1 = Double.pa
If all you want to do is truncate a string at two decimal places, consider using just String functions as shown below:
String s1 = "10.1234";
String formatted = s1;
int numDecimalPlaces = 2;
int i = s1.indexOf('.');
if (i != -1 && s1.length() > i + numDecimalPlaces) {
formatted = s1.substring(0, i + numDecimalPlaces + 1);
}
System.out.println("f1" + formatted);
This saves on parsing into a Double and then formatting back into a String.