Home > Enterprise >  Java multiple modifiers to printformat one variable
Java multiple modifiers to printformat one variable

Time:05-08

I want to print a string for a table, but multiple modifiers are needed for one variable

    System.out.format("%-3s %-20s %-12s %-6s %.2f %-9s\n",
            id, city, date, days, price, vehicle);
}

i want something like this

    System.out.format("%-3s %-20s %-12s %-6s %.2fs %-9s\n",
            id, city, date, days, price, vehicle);
}

but it throws an error because it thinks that the argument will not have 6 but 7 values

CodePudding user response:

It's not entirely clear what you want to do.

Print a number, but expanded to at least 10 'width'

If you want to combine the idea of %.2f (as in, round down to 2 digits 'after the comma'), and the idea of 'this should take up 10 width; if it is less than that, add spaces. If it is more, oof, well, okay, it'll look bad, but then just print more - then, what you're looking for .2f - each 'letter' (%f, %s, etc) has its own set of modifiers, and the modifiers are completely independent (though there are trends). The 10 in s applies specifically to string formatting, you can't tell java: Can you apply this principle (of adding spaces to ensure it always occupies at least 10 width) - but to this completely different formatting letter?

But, the %f formatter also has a width (in fact, they pretty much all day), so use its 'width-setting' feature: .2f.

.2f means:

Format a numeric value. If the value as printed would have more than 2 digits in the fractional section, then round it so that it has 2 digits as a fraction instead; if it has less, use less. In addition, if the complete string (including the fraction part, and the comma) is less than 10 characters long, add spaces to the left side (so that it is right-aligned), so that it is at least 10. If it's more, then it's more, don't try to lop off anything.

You may think that 10.2 would mean: 10 digits, then a comma, then 2 more digits. It does not.

I want to use the same argument more than once in the format string

Perhaps instead you meant: I want one input (say, price) to be used twice in a row. This comes up a lot in date formatting (but, formatting dates with format is weird, so it doesn't come up a lot). The < and 1$ options exist for this purpose. Example:

System.out.format("Your birthday is on: %tY-%tm-%te\n", birthday, birthday, birthday);

%tY prints the year of the argument, tm the month, te the date. However, it's really annoying that we have to pass birthday 3 times in a row. We can fix this:

System.out.format("Your birthday is on: %tY-%<tm-%<te\n", birthday);
// -OR -
System.out.format("Your birthday is on: %tY-%1$tm-%1$te\n", birthday);

The < means: Re-use the same argument. 1$ means: Use the first argument (2$, etc, work just as well). Hence, the above 2 lines produce an identical result.

The docs cover all this in excruciating detail. Next time you have questions, it's generally a good plan to look up the javadoc, read that and follow any links it offers, and only if you're still confused, search the web generally, and only then, if you can't find anything, ask questions on SO.

  • Related