Java String.format(): Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=Internal= * java Format")
 
No edit summary
Line 1: Line 1:
=External=
* http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29
* Format String syntax http://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html#syntax
=Internal=
=Internal=


* [[java Format#Subjects|java Format]]
* [[java Format#Subjects|java Format]]
* [[java SimpleDateFormat| SimpleDateFormat]]
* [[java DecimalFormat|DecimalFormat]]
* [[java MessageFormat|MessageFormat]]
!!!Samples
!!!String
{{{
String arg = "blah"
String.format("%1$-50s", arg);
}}}
This formats the first argument (%1$) as a string "s", to occupy at least 50 characters (width) and it left-justifies it ("-").
For general argument types, the precision is the maximum number of characters to be written to the output.
!!!Floating Point
For the floating-point conversions 'e', 'E', and 'f' the precision is the number of digits after the decimal separator. The conversion is done with rounding:
{{{
double arg = 77.999
String.format("%1$.2f", arg);
}}}
will display "78.00"
!!!Integer
{{{
int arg = 77
String.format("%1$3d", arg);
}}}
will display " 77"
__Referenced by:__\\
[{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]

Revision as of 17:57, 22 January 2016

External

Internal


!!!Samples

!!!String

{{{ String arg = "blah" String.format("%1$-50s", arg); }}}

This formats the first argument (%1$) as a string "s", to occupy at least 50 characters (width) and it left-justifies it ("-").

For general argument types, the precision is the maximum number of characters to be written to the output.

!!!Floating Point

For the floating-point conversions 'e', 'E', and 'f' the precision is the number of digits after the decimal separator. The conversion is done with rounding:

{{{ double arg = 77.999 String.format("%1$.2f", arg); }}}

will display "78.00"


!!!Integer


{{{ int arg = 77 String.format("%1$3d", arg); }}}

will display " 77"



__Referenced by:__\\ [{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]