Is there a way in Java to convert an integer to its ordinal name?

后端 未结 12 1309
生来不讨喜
生来不讨喜 2020-11-27 15:12

I want to take an integer and get its ordinal, i.e.:

1 -> \"First\"
2 -> \"Second\"
3 -> \"Third\"
...
相关标签:
12条回答
  • 2020-11-27 15:38
    private static String getOrdinalIndicator(int number) {
            int mod = number;
            if (number > 13) {
                mod = number % 10;
            }
            switch (mod) {
            case 1:
                return "st";
            case 2:
                return "nd";
            case 3:
                return "rd";
            default:
                return "th";
            }
        }
    
    0 讨论(0)
  • 2020-11-27 15:41

    Another solution

    public static String ordinal(int i) {
        int mod100 = i % 100;
        int mod10 = i % 10;
        if(mod10 == 1 && mod100 != 11) {
            return i + "st";
        } else if(mod10 == 2 && mod100 != 12) {
            return i + "nd";
        } else if(mod10 == 3 && mod100 != 13) {
            return i + "rd";
        } else {
            return i + "th";
        }
    }
    

    Pro: does not require an array to be initialized (less garbage)
    Con: not a one-liner...

    0 讨论(0)
  • 2020-11-27 15:41

    I got a long, complicated one but easy to understand the concept

    private static void convertMe() {
    
        Scanner in = new Scanner(System.in);
        try {
            System.out.println("input a number to convert: ");
            int n = in.nextInt();
    
            String s = String.valueOf(n);
            //System.out.println(s);
    
            int len = s.length() - 1;
            if (len == 0){
                char lastChar = s.charAt(len);
                if (lastChar == '1'){
                    System.out.println(s + "st");
                } else if (lastChar == '2') {
                    System.out.println(s + "nd");
                } else if (lastChar == '3') {
                    System.out.println(s + "rd");
                } else {
                    System.out.println(s + "th");
                }
            } else if (len > 0){
                char lastChar = s.charAt(len);
                char preLastChar = s.charAt(len - 1);
                if (lastChar == '1' && preLastChar != '1'){ //not ...11
                    System.out.println(s + "st");
                } else if (lastChar == '2' && preLastChar != '1'){ //not ...12
                    System.out.println(s + "nd");
                } else if (lastChar == '3' && preLastChar != '1'){ //not ...13
                    System.out.println(s + "rd");
                } else {
                    System.out.println(s + "th");
                }
            }
    
    
        } catch(InputMismatchException exception){
            System.out.println("invalid input");
        }
    
    
    }
    
    0 讨论(0)
  • 2020-11-27 15:41

    Best and Simple way, Here we go:

    import java.util.*;
    public class Numbers 
    {
        public final static String print(int num)
        {
            num = num%10;
            String str = "";
            switch(num)
            {
            case 1:     
                str = "st";
                break;
            case 2:     
                str = "nd";
                break;
            case 3:     
                str = "rd";
                break;
            default: 
                str = "th";             
            }
            return str;
        }
    
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            System.out.print("Enter a number: ");
            int number = sc.nextInt();
            System.out.print(number + print(number));
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:43
    public static String getOrdinalFor(int value) {
             int tenRemainder = value % 10;
             switch (tenRemainder) {
              case 1:
               return value+"st";
              case 2:
               return value+"nd";
              case 3:
               return value+"rd";
              default:
               return value+"th";
             }
            }
    
    0 讨论(0)
  • 2020-11-27 15:44

    In 1 line:

    public static String ordinal(int i) {
        return i % 100 == 11 || i % 100 == 12 || i % 100 == 13 ? i + "th" : i + new String[]{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"}[i % 10];
    }
    
    0 讨论(0)
提交回复
热议问题