I am writing a program and am stuck in the following requirement:
The output goes to a fixed format file
Total 1 Total 2
---------------- ----------------
311,862.33 23,456.00
40,000.00 789,123.00
As in above example, the length of the amount string varies.
Requirement is that the amount should always start at the end of the 'hyphens'.
How to achieve this ?
I know that right and left pad to the string could be acheived through:
String.format("%1$#20s", "311,862.33");
String.format("%1$-15s", "12345");
But how to achieve in my case where there are multiple strings in every line and the string lengths are dynamic ?
Found below code which formats amount in a single column. But how to achieve the same for multiple columns i.e. multiple amounts in same line ?
String format = "%10.2f\n"; // width == 10 and 2 digits after the dot
float [] floats = {123.45f, 99.0f, 23.2f, 45.0f};
for(int i=0; i<floats.length; i++) {
float value = floats[i];
System.out.format(format, value);
}
This will give output as:
123.45
99.00
23.20
45.00
Thanks for reading!