Return the ordinal for an integer, e.g. - Java java.lang

Java examples for java.lang:int Format

Description

Return the ordinal for an integer, e.g.

Demo Code


//package com.java2s;

public class Main {
    /**/*ww w  .jav  a  2  s.  com*/
     * Return the ordinal for an integer, e.g. getOrdinalFor(1) returns 1st
     * @param value cardinal integer for which an ordinal will be returned
     * @return ordinal of cardinal integer
     */
    public static String getOrdinalFor(int value) {
        int hundredRemainder = value % 100;
        int tenRemainder = value % 10;
        if (hundredRemainder - tenRemainder == 10) {
            return value + "th";
        }

        switch (tenRemainder) {
        case 1:
            return value + "st";
        case 2:
            return value + "nd";
        case 3:
            return value + "rd";
        default:
            return value + "th";
        }
    }
}

Related Tutorials