Java String.format(): Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
 
* Format String syntax: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Formatter.html#syntax
* http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29
* 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=
Line 10: Line 9:
* [[java DecimalFormat|DecimalFormat]]
* [[java DecimalFormat|DecimalFormat]]
* [[java MessageFormat|MessageFormat]]
* [[java MessageFormat|MessageFormat]]
=Overview=


Format string:
<font size=-1>
%[argument_index$][flags][width][.precision]conversion
</font>


=String=


!!!Samples
<pre>
 
!!!String
 
{{{
String arg = "blah"
String arg = "blah"
String.format("%1$-50s", arg);
String.format("%1$-50s", arg);
}}}
</pre>


This formats the first argument (%1$) as a string "s", to occupy at least 50 characters (width) and it left-justifies it ("-").
This formats the first argument (%1$) as a string "s", to occupy at least 50 characters (width) and it left-justifies it ("-").
Line 26: Line 27:
For general argument types, the precision is the maximum number of characters to be written to the output.
For general argument types, the precision is the maximum number of characters to be written to the output.


!!!Floating Point
==Parameterized Width==
 
<pre>
int width=10;
String format = "%1$-" + width + "s";
String.format(format, s);
</pre>
 
=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:
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:


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


will display "78.00"
will display "78.00"


 
=Integer=
!!!Integer
<syntaxhighlight lang='java'>
 
int arg = 77.
 
{{{
int arg = 77
String.format("%1$3d", arg);
String.format("%1$3d", arg);
}}}
</syntaxhighlight>
 
will display " 77"
will display " 77"


=Hexadecimal Integer=
Display a argument as a 0-padded hexadecimal integer on at least 2 characters. Format string:
<syntaxhighlight lang='java'>
int i = 10;
String.format("%1$02x", i);
</syntaxhighlight>
will display "0a".


 
The result will be 0-padded (<code>flag</code> is "0"), will be displayed on two characters and more (<code>width</code> is 2) and it will be rendered as a hexadecimal (<code>conversion</code> is "x").
 
__Referenced by:__\\
[{INSERT com.ecyrd.jspwiki.plugin.ReferringPagesPlugin WHERE max=20, maxwidth=50}]

Latest revision as of 17:58, 26 August 2021

External

Internal

Overview

Format string:

%[argument_index$][flags][width][.precision]conversion

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.

Parameterized Width

int width=10;
String format = "%1$-" + width + "s";
String.format(format, s);

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"

Hexadecimal Integer

Display a argument as a 0-padded hexadecimal integer on at least 2 characters. Format string:

int i = 10;
String.format("%1$02x", i);

will display "0a".

The result will be 0-padded (flag is "0"), will be displayed on two characters and more (width is 2) and it will be rendered as a hexadecimal (conversion is "x").