Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Something like</p> <pre><code>String.format("%10.2f", yourFloat) // or System.out.format("%10.2f", yourFloat) </code></pre> <p>Will print a 10-character wide (including decimal) string, with two numeric characters after the decimal.</p> <p>(<a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format%28java.lang.String,%20java.lang.Object...%29" rel="nofollow">Docs</a>)</p> <p>So </p> <pre><code>String.format("$%6.2f", value[i]) </code></pre> <p>will align both the <code>$</code> and <code>.</code> characters (unless <code>value[i] &gt; 999.99</code>).</p> <hr> <p>Instead of:</p> <pre><code>System.out.println( num[i] +"\t "+ qty[i] +"\t "+ money.format(price[i])+"\t"+ money.format(value[i])+"\t"+ reorder[i]); </code></pre> <p>(which is exactly what you had, just formatted for clarity and to remove the scroll bar)</p> <p>I'd probably write:</p> <pre><code>System.out.format("%5d\t %5d\t $%5.2f\t $%6.2f\t %5d %n", num[i], qty[i], price[i], value[i], reorder[i]); </code></pre> <p>This assumes that the <code>price</code> and <code>value</code> arrays are floats or doubles. Since <code>money</code> isn't a standard class, it's hard to tell exactly what it does other than add a <code>$</code> sign.</p> <hr> <p>The string format syntax is defined in the docs, but for floats it's <strong>roughly</strong>:</p> <pre><code>%X.Yf </code></pre> <p>Where <code>X</code> is the total field width and <code>Y</code> is the number of decimal points</p> <p>For example</p> <pre><code>"123.40" Has a total width of 6: 3 + 1 [decimal point] + 2 = 6) " 2.34" Also has a total width of 6: 2 [spaces] + 1 + 1 [decimal point] + 2 = 6 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload