Android examples for java.lang:String Format
Gets the ordinal (1st) for the given cardinal (1)
import android.graphics.Typeface; import android.text.Spannable; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.style.StyleSpan; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; public class Main{ /**//w w w . ja v a 2s . c o m * * * @param cardinal * @return */ public static String getOrdinal(int cardinal) { if (cardinal < 0) { return "-th"; } String c = String.valueOf(cardinal); String n = "0"; if (c.length() > 1) { n = c.substring(c.length() - 2, c.length() - 1); } String l = c.substring(c.length() - 1); if (!n.equals("1")) { if (l.equals("1")) { return c + "st"; } else if (l.equals("2")) { return c + "nd"; } else if (l.equals("3")) { return c + "rd"; } } return c + "th"; } }