View Single Post
Old 03-04-2004, 08:46 AM   PM User | #12
liorean
The thread killer


 
Join Date: Feb 2003
Location: Umeå, Sweden
Posts: 5,575
Thanks: 0
Thanked 84 Times in 75 Posts
liorean will become famous soon enoughliorean will become famous soon enough
Q: How do I add ordinals (st, nd, rd, th) to a number?

A: After quite some optimisation, you could end up with a function looking something like this:
Code:
Number.prototype.toOrdinal=function(m){
    return (this +
        ["th","st","nd","rd"][(!(
            ((m=this%10) >3) ||
            (Math.floor(this%100/10)==1)
        ))*m]);
}
and you use it like this:
Code:
var
    nNumber=12,
    sFirstOrdinal=nNumber.toOrdinal(), // => '12th'
    sSecondOrdinal=(22).toOrdinal(); // => '22nd'
Well, I won't bore you with the development procedure of this very slick and optimised function. You can check it out yourself in this thread.

Last edited by liorean; 03-04-2004 at 08:50 AM..
liorean is offline