String twoDecimal(final float f) { return new DecimalFormat("#.##").format(f); }And for doubles its exactly the same:
String twoDecimal(final double d) { return new DecimalFormat("#.##").format(d); }Example use:
void exampleUse() { System.out.println("value="+twoDecimal(45.123456)); }