[removed] Ordinal suffix for numbers with specific CSS class

前端 未结 8 1507
时光说笑
时光说笑 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:11

    function ordsfx(a){return["th","st","nd","rd"][(a=~~(a<0?-a:a)%100)>10&&a<14||(a%=10)>3?0:a]}

    See annotated version at https://gist.github.com/furf/986113#file-annotated-js

    Short, sweet, and efficient, just like utility functions should be. Works with any signed/unsigned integer/float. (Even though I can't imagine a need to ordinalize floats)

提交回复
热议问题