 <pre><code>String ordinal(int num) { String[] suffix = {"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}; int m = num % 100; return String.valueOf(num) + suffix[(m &gt; 10 &amp;&amp; m &lt; 20) ? 0 : (m % 10)]; } </code></pre>