You can write a function that prints spaces before the number.
If they are all going to be 4.3f, we can assume that each number will take up to 8 characters, so we can do that:
public static String printDouble(Double number) { String numberString = String.format("%4.3f", number); int empty = 8 - numberString.length(); String emptyString = new String(new char[empty]).replace('\0', ' '); return (emptyString + numberString); }
Input:
public static void main(String[] args) { System.out.println(printDouble(1.123)); System.out.println(printDouble(12.423)); System.out.println(printDouble(0.123)); System.out.println(printDouble(123.344)); System.out.println(printDouble(1.100)); }
Output:
run: 1,123 12,423 0,123 123,344 1,100 BUILD SUCCESSFUL (total time: 0 seconds)