The following sample code:
System.out.printf(" %1.1f " , 2122.3856);
Prints out 2122.4
The format i specified for the above result shows that i asked for one decimal point and 1 character long for the width. I can see the one decimal point (i.e. .4) but why did the specified width not work?
The
widthspecification is the minimum, not the maximum.If you want to enforce a width that essentially makes the number’s value meaningless, you should either do math (like to get the tenths place) or manipulate a conversion to string.