Ask your own question, for FREE!
Computer Science 20 Online
OpenStudy (javk):

What does String.format() do in java. what is being formatted and how is it being formatted?

OpenStudy (woodrow73):

More or less, you format values into a String. For example ``` double a = 2.11, b = 5.23125, c = 24.2284; System.out.println( String.format("val 1: %.1f%nval 2: %.2f%nval 3: %.2f%n", a, b, c) ); ``` Will output ``` val 1: 2.1 val 2: 5.23 val 3: 24.23 ``` In the first argument of String.format, the number of arguments the method expects depends on how many you ask for. % is a special key, and something like %f I think is called a format specifier. %f will expect a floating point number, %d would expect an integer (probably of int, short, and long datatypes), %c char, %s for string (note doing %C or %S will auto-format the value to upper-case) ``` System.out.printf("%f%n", 4.2232323); System.out.printf("%f%f%f", 1.1, 2.2, 3.3); int age = 5; System.out.printf("I am %d years old", age); /* prints 4.223232 1.1000002.2000003.300000 I am 5 years old notice that the order of the arguments is maintained in the string */ ``` note that System.out.printf(); has the same parameter/argument system as String.format, except it doesn't return anything, and prints the outputted String to console. Also note that I think \n and %n have the same function of newline character. There is more to the syntax that allows for greater ability to format, like how I rounded the floating point numbers in the first example. syntax: %[flags][width][.precision]coversion All format specifiers begin with % character flags- after the % character, one or more optional flags may appear. Flags cause the value to be formatted in a variety of ways. width- after any flags, you can optionally specify the minimum field width for the value .precision- If the value is a floating-point number, after the minimum field width, you can optionally specify the precision. This is the number of decimal places that the number should be rounded to conversion- All format specifiers must end with a conversion character, such as f for floating-point, or d for decimal integer ----------------------------------- Here is a good reference for the different values you can use: https://www.cs.colostate.edu/~cs160/.Spring16/resources/Java_printf_method_quick_reference.pdf The comma flag is pretty helpful ``` System.out.printf("$%,.2f", 5900000.); //. makes number a double //prints: $5,900,000.00 ``` And leveraging the width can lead to nicer formatting. To reformat the first example: ``` System.out.println( String.format("val 1: %5.2f%nval 2: %5.2f%nval 3: %5.2f\n", a, b, c) ); /* prints: val 1: 2.11 //2.11 is 1 char short of 5, space added to left val 2: 5.23 val 3: 24.23 //decimals line up in monospace font. note 1 space to left */ ``` ``` //Another example, using various 'conversion characters': double netWorth = 5900000; char currency = '$'; System.out.printf("Name: %S%nAge: %d%nNet Worth: %c%,.2f%n", "woodrow73", 5, currency, netWorth); /* prints: Name: WOODROW73 Age: 5 Net Worth: $5,900,000.00 */ ```

OpenStudy (woodrow73):

Using the 0 flag would probably make the numbers look better in a non-monospace font; 0 : forces numerical values to be zero-padded ( default is blank padding ) ``` System.out.println( String.format("val 1: %05.2f%nval 2: %05.2f%nval 3: %05.2f\n", a, b, c) ); /* prints: val 1: 02.11 val 2: 05.23 val 3: 24.23 */ ```

OpenStudy (javk):

Thank you! I used the same methodology to format the fields in an object and it worked beautifully.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!