View Single Post
Old 08-18-2003, 03:24 PM   PM User | #6
Graeme Hackston
Regular Coder

 
Join Date: Jun 2002
Posts: 624
Thanks: 0
Thanked 0 Times in 0 Posts
Graeme Hackston is an unknown quantity at this point
Or, you can use typeof right in the function but this is adding yet another condition for the function to check.

If you use it like this you can remove .toString() from the function calls in Johns script.
Code:
function num_abbrev_str(num) {
if (typeof num == 'number') {
num = num.toString()
}
var len = num.length, last_char = num.charAt(len - 1), abbrev
if (len == 2 && num.charAt(0) == '1') {
abbrev = 'th'
} else {
if (last_char == '1') {
abbrev = 'st'
} else if (last_char == '2') {
abbrev = 'nd'
} else if (last_char == '3') {
abbrev = 'rd'
} else {
abbrev = 'th'
}
}
return num + abbrev
}

alert(num_abbrev_str('22'))

alert(num_abbrev_str(22))
Graeme Hackston is offline   Reply With Quote