AS3/Flex toOrdinal

Posted by Alexis on 24 January 2011 | 0 Comments

Tags: ,

Over the weekend I had to put together a small Flex project to illustrate data in a pie graph. Also the data had to read in terms of its placement 1st 2d 3rd or 4th. While there are Flex validators for these types of things I prefer to write in Actionscript. I find it easier to follow and easily reusable in other non Flex projects.

There is a great PHP function toOdinal() which I converted to AS3 for anyone else trying to do a similar things here it is.

 private function toOrdinal(cdnl:Number):String {
    var test_c:Number = Math.abs(cdnl) % 10;
    var ext:String = ((Math.abs(cdnl) % 100 < 21 && Math.abs(cdnl) % 100 > 4) ? 'th'
        : ((test_c < 4) ? (test_c < 3) ? (test_c < 2) ? (test_c < 1)
            ? 'th' : 'st' : 'nd' : 'rd' : 'th'));
    return cdnl + ext;
}

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments