I need to format a decimal value into a string where I always display at lease 2 decimals and at most 4.
So for example
\"34.49596\" would be \"34.4
Here is a small code snippet that does the job:
double a = 34.51234; NumberFormat df = DecimalFormat.getInstance(); df.setMinimumFractionDigits(2); df.setMaximumFractionDigits(4); df.setRoundingMode(RoundingMode.DOWN); System.out.println(df.format(a));