The way we count numbers in English appears quite strange (I am sure) to those who don't speak it natively. When we refer to dates we often add "st", "rd", "nd" or "th" to the end of any numbers. Example: 10th February, 23rd June
I remember one solution I used for solving this "problem" early in my career. There can be up to 31 days in a month, so I created an array and populated it with the correct endings:
And I would then get the correct ending by calling the index of the array with the number directly:
My example contains no attempt at error checking (and this is certainly not appropriate for production quality code).
I've used much the same code server-side as well as client-side and it's done the job well. I am interested in how others have achieved a similar result.
Does anyone have any significantly different mechanisms to do this? Want to share?
Cheers,
Jeff
[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
What is Javascript? faq216-6094
I remember one solution I used for solving this "problem" early in my career. There can be up to 31 days in a month, so I created an array and populated it with the correct endings:
Code:
var numberWords = ['X','st','nd','rd','th','th','th','th','th','th','th','th','th','th',
'th','th','th','th','th','th','th','st','nd','rd','th','th','th','th','th','th','th','st'];
Code:
function getNumberExt(theNumber) {
return (theNumber+numberWords[theNumber]);
}
alert(getNumberExt(23)); // alerts 23rd
I've used much the same code server-side as well as client-side and it's done the job well. I am interested in how others have achieved a similar result.
Does anyone have any significantly different mechanisms to do this? Want to share?
Cheers,
Jeff
[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
What is Javascript? faq216-6094