Obviously, once you have a formatted String there are many things that you can do with it, including printing it (e.g., with either the print() or println() methods, both of which have a single String parameter. format ( "Year: %4d, Sales: $%,14.2f", year, sales ) Īfter this code is executed, s will contain Year: 2017, Sales: $ 1,050,987.00 Printing a Formatted String Int year = 2017 double sales = 1050987.00 String s This is illustrated in the following example. The format string in the invocation of the format() method should have the same number of format specifiers as there are other parameters in the invocation. Similarly, the format specifier -%6.2f indicates a double that will be left-justified in a field of width 6 with 2 places to the right of the decimal point. So, for example the format specifier %8d indicates an int that will be right-justified in a field of width 8. The optional precision determines the nnumbenr of digits to the right of the decimal point for double values. The optional width determines the total number of characters that this portion of the String will contain. % conversionĬommon values of conversion include b for boolean, c for char, d for int, e for double in scientific notation, f for double, and s for String.Ĭommon values of the optional flag include - for left-justified, + to always include the sign, 0 to pad with zeros,, to use grouping separators, ( to put negative numbers in parentheses, and a space character to use a space for the sign of a postive number (as opposed to omitting it).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |