[removed] Ordinal suffix for numbers with specific CSS class

前端 未结 8 1396
时光说笑
时光说笑 2021-02-14 03:01

I am trying to display numbers within a particular table with ordinal suffixes. The table always shows three numbers which come from an XML file. The numbers show ranks, so for

8条回答
  •  眼角桃花
    2021-02-14 03:18

    function nth(n){
        if(isNaN(n) || n%1) return n;   
        var s= n%100;
        if(s>3 && s<21) return n+'th';
        switch(s%10){
            case 1: return n+'st';
            case 2: return n+'nd';
            case 3: return n+'rd';
            default: return n+'th';
        }
    }
    

    You can take care of the teens in their own line, other integers follow the last digit rules.

提交回复
热议问题