you can use paramaters, eg: %1.2f
, but this round the number. I'm not sure is that what you want.
float f = 1.225f;
System.out.printf("%f \t %1.2f \t %1.4f", f,f,f);
you can you BigDecimal
is better solution, but problem with zeros on the end.
float f = 1.225f;
BigDecimal number1 = new BigDecimal(f);
BigDecimal number2 = new BigDecimal(f).setScale(2, RoundingMode.HALF_DOWN);
BigDecimal number3 = new BigDecimal(f).setScale(4, RoundingMode.HALF_DOWN);
System.out.println(number1+"\n"+number2+"\n"+number3);
but if you put in BigDecimal number as string, will be printed with precission of this string:
BigDecimal bd1 = new BigDecimal("1.123");
BigDecimal bd2 = new BigDecimal("1.123000");
System.out.println(bd1+"\n"+bd2);
so you can use this trick to your case:
float f = 1.225000f;
BigDecimal snumber1 = new BigDecimal(f+"");
System.out.println(snumber1);