Tales of an IT Nobody

devbox:~$ iptables -A OUTPUT -j DROP

Add “th” (“nd”, “st”) to a number … painlessly – Part 2 April 26, 2011

So in the previous post I posted my recipe for adding rank suffixes to numbers.

Sometimes – a simple problem can be frustratingly goofy to solve. I took a lazy shortcut by passing the number through mktime – then to date to do it for me.

After analyzing what I came up with – it occurred to me that my final approach could be streamlined even better.

Logically we add ‘st,nd,rd’ to numbers 1-3 in all circumstances except when they’re in what I call an ‘eleventh’ verbal range – meaning ‘111’, ‘213’ – we simply do ‘th’.

Since I’ve already determined the wrap around (mod 10) – I gathered maybe just a straight up switch statement would be faster in the big picture …

So here’s the 2nd rendition of my function:

It’s longer for sure – but when run through a simple benchmark:
10 passes of a 100,000 iteration loop the results are pretty clear on how mktime+date can drag it down, it’s not the most scientific method but the gap is big enough to call the results conclusive:

Version 1 (With mktime+date)
Time: 7.79 seconds

Version 2 (See above – switch)
Time: 4.81 seconds

For some reason – solution #2 doesn’t feel as ‘cool’ as the first solution; I like the hackish appeal of it, but there seems to be more elegance in the simplicity, with more truth in it’s nature.

No Comments on Add “th” (“nd”, “st”) to a number … painlessly – Part 2
Categories: php programming

Add “th” (“nd”, “st”) to a number … painlessly. April 25, 2011

There’s not enough out there about this trick – but you don’t need a convoluted solution to add rank indicators to a number, so to add more noise to google search, here my variant is:

Make sure you visit part 2!

Logical breakdown:

First we get mod of $n against 10 – essentially stripping away base 10 from rolling over.

Then, since we word 1 = 1st, 2 = 2nd, 3 = 3rd – see if we have to do anything special for numbers with modulo in that range – otherwise anything from 4-9,0 is nth.

Additionally, we take characters in the ‘eleventh’ verbal range and simply make them all ‘nth’ – so we skip our special date logic and cut straight to $n.’th’.

We check this by sloppily looking for the 2nd to the last character of our number using substr. If it is NOT 1 (Indicating an ‘eleventh’) – we take the logic block for using date to deal with the markup (st, nd, rd)

The only exception to all of this is the substr will wrap the number ‘1’ and take the logic, so we make sure that if it’s explicit value is 1 – we take the logic as well to indicate ‘1st’….

In hindsight… I will expand on whether or not using date() + mktime() is worth it versus the extra effort for manually figuring the st/nd/rd suffixes.

Follow up: I have made a new rendition with speed comparisons

No Comments on Add “th” (“nd”, “st”) to a number … painlessly.
Categories: php programming

libflashplayer.so – quickie for install. April 11, 2011

Need a place to put it on debian/ubuntu?


Chrome: (You might have to create the ‘plugins’ dir – I did =\ )


Restart all instances of the browser/s and check it out.

PS: Flash on 64-bit linux doesn’t matter, you can put it in lib64/ if you -REALLY- want..

No Comments on libflashplayer.so – quickie for install.
Categories: linux purdy