<blockquote><p>In Java, you can use the printf method to output the alignment, and control the format and alignment through format specifiers, including: left alignment, adding the sign 0 before the signed number, filling the gaps with zeros < right alignment> Align left and fill empty spaces with spaces^ Center align</p></blockquote>
<p><img src="https://img.php.cn/upload/article/202404/21/2024042102282239814.jpg" alt="How to align output in java" ></p>
<p><strong>How to align output in Java</strong></p>
<p>In Java, we Alignment can be output using the <code>printf</code> method. The <code>printf</code> method provides a variety of format specifiers to control the format and alignment of the output. </p>
<p><strong> Syntax: </strong></p>
<pre class="brush:php;toolbar:false"><code class="java">printf(String format, Object... args);</code></pre>
<p><strong> Parameters: </strong></p>
<ul>
<li>
<code>format</code>: format string , which contains placeholders (for example, <code>%d</code>, <code>%s</code>). </li>
<li>
<code>args</code>: The actual parameters to be output. </li>
</ul>
<p><strong>Alignment specifier: </strong></p>
<table>
<thead><tr>
<th>Specifier</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td><code>-</code></td>
<td>Left aligned</td>
</tr>
<tr>
<td><code> </code></td>
<td> Precede signed numbers </td>
</tr>
<tr>
<td><code>0</code></td>
<td> Fill empty spaces with zeros </td>
</tr>
<tr>
<td><code><</code></td><td>Align right</td></tr><tr><td><code>></code></td>
<td>Align left and fill the gaps with spaces</td>
</tr>
<tr>
<td><code>^</code></td>
<td>Centered alignment</td>
</tr>
</tbody>
</table>
<p><strong>Example: </strong></p>
<p>To right-justify the integer <code>123</code> and fill the gaps with spaces, you can write: </p>
<pre class="brush:php;toolbar:false"><code class="java">System.out.printf("%10d", 123);</code></pre>
<p>This will output: </p>
<pre class="brush:php;toolbar:false"><code> 123</code></pre>
<p>To right-justify the floating point number<code> 123.45</code> To center align and retain two decimal places, you can write: </p>
<pre class="brush:php;toolbar:false"><code class="java">System.out.printf("%12.2f", 123.45);</code></pre>
<p>This will output: </p>
<pre class="brush:php;toolbar:false"><code> 123.45</code></pre>
<p>To left align the string "Hello" and fill the gaps with asterisks, you can Write like this: </p>
<pre class="brush:php;toolbar:false"><code class="java">System.out.printf("%-20s", "Hello");</code></pre>
<p> This will output: </p>
<pre class="brush:php;toolbar:false"><code>Hello****************</code></pre>
The above is the detailed content of How to align output in java. For more information, please follow other related articles on the PHP Chinese website!
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn