This shows you the differences between two versions of the page.
java:io:printstream:format [2012/11/23 13:46] 127.0.0.1 external edit |
java:io:printstream:format [2024/02/16 01:12] (current) |
||
---|---|---|---|
Line 2: | Line 2: | ||
====== format ====== | ====== format ====== | ||
format args to the format string format and write the result to the stream | format args to the format string format and write the result to the stream | ||
- | |||
<code java> | <code java> | ||
format(String format, Object... args); | format(String format, Object... args); | ||
Line 8: | Line 7: | ||
return value: stream | return value: stream | ||
</code> | </code> | ||
+ | |||
+ | ^ sign ^ Description ^ | ||
+ | |0 |if the place is not busy they prints a 0 | | ||
+ | |# |if the place is not busy they prints nothing | | ||
+ | |. |decimal separator Separates and decimal places. | | ||
+ | |, |Groups the numbers (a group is as large as the distance from "," to "."). | | ||
+ | |; |Delimiter. To the left of the pattern is positive, the right for negative numbers. | | ||
+ | |- |The default character for the negative prefix | | ||
+ | |% |The number is multiplied by 100 and reported as a percentage. | | ||
+ | |% % (without the space )|Just like %, with only thousandths. | | ||
+ | |\uXXXX |unicode XXXX stands for Numbers and chars like \u20B3 | | ||
+ | |' |Masking of special symbols in the prefix or suffix | | ||
+ | |%n |new line | | ||
+ | |%b |Boolean | | ||
+ | |%% |Percent | | ||
+ | |%s |String | | ||
+ | |%c |Unicode-Char | | ||
+ | |%d |decimaldigit | | ||
+ | |%x |hexadecimal | | ||
+ | |%t |Date / Time | | ||
+ | |%f |Float | | ||
+ | |%e |scientific notation | | ||
+ | |||
===== Language Example ===== | ===== Language Example ===== | ||
<code java> | <code java> | ||
+ | package codereferececomjava; | ||
+ | |||
+ | public class CodeRefereceComJava { | ||
- | String strout = null; | ||
- | String str = "is the answer"; | ||
- | int i = 42; | ||
- | String[] args = { "way", "example" }; | ||
- | strout = String.format("%d %s", i, str); | + | public static void main(String[] args) { |
- | System.out.println(strout); | + | |
+ | String strout = null; | ||
+ | String str = "is the answer"; | ||
+ | int i = 42; | ||
+ | String[] arguments = { "way", "example" }; | ||
+ | |||
+ | strout = String.format("%d %s", i, str); | ||
+ | System.out.println(strout); | ||
+ | |||
+ | strout = String.format( "this is another %s for this %s.\n", arguments ); | ||
+ | System.out.println(strout); | ||
+ | |||
+ | double ans = 42.1234568; | ||
+ | |||
+ | System.out.format("The answer is probably %.0f", ans); | ||
+ | System.out.format("\t and not %.2f \n", ans); | ||
+ | System.out.format("also not %1.2f \n", ans); // prints 1 | ||
+ | |||
+ | |||
+ | String string = "hey"; | ||
+ | System.out.format("%-10s reserve 10 signs on the right side\n", string ); | ||
+ | System.out.format("%20s reserve 10 signs on the left side\n", string ); | ||
+ | |||
+ | System.out.format("%10d reserve 10 signs for the number \n", i ); | ||
+ | System.out.format("%+d print math signs \n", i ); // print math signs | ||
+ | System.out.format("%010d reserve 10 places and fill it with 0\n", i ); | ||
+ | |||
+ | System.out.format("%,2.3f special character \n", ans ); | ||
+ | |||
+ | |||
+ | } | ||
+ | } | ||
- | strout = String.format( "this is another %s for this %s.", args ); | ||
- | System.out.println(strout); | ||
- | |||
</code> | </code> | ||
==== output ==== | ==== output ==== | ||
- | 42 is the answer | + | The answer is probably 42 and not 42,12 |
- | this is another way for this example. | + | also not 42,12 |
+ | hey reserve 10 signs on the right side | ||
+ | hey reserve 10 signs on the left side | ||
+ | 42 reserve 10 signs for the number | ||
+ | +42 print math signs | ||
+ | 0000000042 reserve 10 places and fill it with 0 | ||
+ | 42,123 special character | ||
+ | |||
+ | ===== 2nd format example in java ===== | ||
+ | <code java> | ||
+ | package codereferececomjava; | ||
+ | |||
+ | public class CodeRefereceComJava { | ||
+ | |||
+ | |||
+ | public static void main(String[] args) { | ||
+ | int dnumber = - 5; | ||
+ | double fnumber = 12317.27; | ||
+ | String str = "Number"; | ||
+ | char ch = 'A'; | ||
+ | System.out.format("%-30s : %-+10d : %c : %,15.1f\n", str, dnumber, ch, fnumber); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | </code> | ||
+ | |||
+ | === output of 2nd format example === | ||
+ | Number : -5 : A : 12.317,3 |