Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-25-2003, 10:23 PM   PM User | #16
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
Code:
function num_abbrev_str(n)
{
 return n + (( Math.floor(n/10) == 1) ? "th" : ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"][n%10]);
}
RoyW is offline   Reply With Quote
Old 11-25-2003, 10:26 PM   PM User | #17
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
Whoa.

That works for dates, doesn't work for 113
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)
whammy is offline   Reply With Quote
Old 11-25-2003, 10:46 PM   PM User | #18
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
Yeh I wasn't thinking above dates.
I think liorean has the best solution
RoyW is offline   Reply With Quote
Old 11-25-2003, 10:47 PM   PM User | #19
jsWalter
New Coder

 
Join Date: Nov 2003
Location: Chicago, IL
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
jsWalter is an unknown quantity at this point
no, but this does!

function num_abbrev_str(n)
{
i = n % 100;

return n + (( Math.floor(i/10) == 1) ? "th" : ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"][i%10]);
}


jsWalter
jsWalter is offline   Reply With Quote
Old 11-25-2003, 11:13 PM   PM User | #20
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
Hi jsWalter,
You beat me to it. I was going to do it in one step
Code:
function num_abbrev_str(n)
{
 return n + (( Math.floor((n%100)/10) == 1) ? "th" : ["th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"][n%10]);
}
but it is essentially the same thing.

I just noticed that liorean is assuming that in boolean expressions true==1

( this % 100 - nModTen != 10 )

Should evaluate to 0 or 1 and then multiply it by a value.

Hmm sneaky
(but maybe a little dangerous)
RoyW is offline   Reply With Quote
Old 11-25-2003, 11:59 PM   PM User | #21
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
Yah, I know it's sneaky. It's one of the tricks I thought of when I read through the JavaScript and ECMAScript specs recently, and this is the first time I've got to put it to use. Try it: <javascript:alert(Number(Boolean(2)));void(0);>

Oh, just wondering - what might be fastest:
Code:
(( Math.floor((n%100)/10) == 1)
With one scope jump, one property lookup, one function call, one remainder, one division and one comparison, or
Code:
( this % 100 - nModTen != 10 ) * nModeTen
With one remainder, one subtraction, one comparison, one autocasting, and one multiplication.

I believe your code would win out, in those cases. (The access part (scope jump and property lookup) should be fast compared to my math, and the function call and the autocast should both be almost unnoticable) However, my single comparison should make my code faster in the cases of 'th', when I won't have to perform all those numerical operations. So, how would they compare for many numbers, performance wise?
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

Last edited by liorean; 11-26-2003 at 12:08 AM..
liorean is offline   Reply With Quote
Old 11-26-2003, 12:36 AM   PM User | #22
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
This is the kind of discussion that totally rules.
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)
whammy is offline   Reply With Quote
Old 11-26-2003, 01:00 AM   PM User | #23
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
Oh, having a look at your code, this change saves space, but I think it makes for slightly worse performance. I think it's cleaner looking, though:
Code:
function num_abbrev_str(n)
{
 return n + (( Math.floor((n%100)/10) == 1) ? "th" : ([, "st", "nd", "rd", ,,,,,][n%10]) || 'th');
}
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 11-26-2003, 04:44 AM   PM User | #24
RoyW
Regular Coder

 
Join Date: Jun 2002
Location: Atlanta, GA.
Posts: 313
Thanks: 0
Thanked 0 Times in 0 Posts
RoyW is an unknown quantity at this point
I like the boolean trick and I like the idea of only having 4 strings in the array
Code:
function num_abbrev_str(n)
{
    return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10]; 
}
In this 60% of the time the
(n%10>3)
will be true so the
Math.floor(n%100/10)==1
will only be executed for 40% of the numbers.

Invert the boolean and do the multiply
NOTE: I have utilised operator precedence to reduce char count.
Here is a version with parentheses to show precedence
PHP Code:
function num_abbrev_str(n)
{
    return 
+ ["th","st","nd","rd"][(!( ((n%10) >3) || (Math.floor(n%100/10)==1)) ) * (n%10)]; 

RoyW is offline   Reply With Quote
Old 11-26-2003, 04:58 AM   PM User | #25
whammy
Senior Coder

 
Join Date: Jun 2002
Location: 41° 8' 52" N -95° 53' 31" W
Posts: 3,660
Thanks: 0
Thanked 0 Times in 0 Posts
whammy is an unknown quantity at this point
I personally think that's a work of art...
__________________
Former ASP Forum Moderator - I'm back!

If you can teach yourself how to learn, you can learn anything. ;)

Last edited by whammy; 11-26-2003 at 05:04 AM..
whammy is offline   Reply With Quote
Old 03-18-2004, 03:05 AM   PM User | #26
shlagish
Senior Coder

 
Join Date: Apr 2003
Location: Canada
Posts: 1,063
Thanks: 2
Thanked 0 Times in 0 Posts
shlagish is an unknown quantity at this point
I personnaly thought I knew something about javascript. I now think I know nothing !
__________________
Shawn
shlagish is offline   Reply With Quote
Old 10-20-2005, 10:14 AM   PM User | #27
r0n1n
New to the CF scene

 
Join Date: Oct 2005
Location: Torquay
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
r0n1n is an unknown quantity at this point
regexp

I know it's PHP but I thought somone was bound to know the JavaScript syntax for the regexp check.

<?php

$days = range(1, 31);

foreach($days as $day)
{
if(ereg("(^[23]?1$)", $day))
{
$suff = "st";
}
elseif(ereg("(^2?2$)", $day))
{
$suff = "nd";
}
elseif(ereg("(^2?3$)", $day))
{
$suff = "rd";
}
else
{
$suff = "th";
}

echo $day . $suff . "<br />";
}
?>

Last edited by r0n1n; 10-20-2005 at 11:36 AM.. Reason: code sucked
r0n1n is offline   Reply With Quote
Old 10-20-2005, 11:55 AM   PM User | #28
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
That really was an old thread you dug up...

This problem is much less complex when handling it as numbers compared to handling it as strings. Math is generally easier to work with than string comparisons, especially when it's a numerical problem.
__________________
liorean <[lio@wg]>
Articles: RegEx evolt wsabstract , Named Arguments
Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards
liorean is offline   Reply With Quote
Old 10-20-2005, 11:32 PM   PM User | #29
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,453
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
What about this to test the values in the easiest to follow way:

switch (aNum) {
case 1:
case 21:
case 31:
suffix = 'st';
break;
case 2:
case 22:
suffix = 'nd';
break;
case 3:
case 23:
suffix = 'rd';
break;
default:
suffix = 'th';
}
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is online now   Reply With Quote
Old 03-26-2010, 02:11 PM   PM User | #30
bardonw
New Coder

 
Join Date: Dec 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
bardonw is an unknown quantity at this point
I think that this is an interesting discussion. I see that there are a number of solutions for this problem. I think, though, that it is always important to generalize when developing script code that could be used in a number of situations. That said, I wrote a short function which will do the trick for any positive or negative integer. The code can be found at http://gotochriswest.com/development...OrdinalFor.php. Alternatively, you can also find it below:
Code:
Number.getOrdinalFor = function(intNum, includeNumber)
{
	return (includeNumber ? intNum : "") + (((intNum = Math.abs(intNum) % 100)
		% 10 == 1 && intNum != 11) ? "st" : (intNum % 10 == 2 && intNum != 12)
		? "nd" : (intNum % 10 == 3 && intNum != 13) ? "rd" : "th");
};

Last edited by bardonw; 03-26-2010 at 03:07 PM.. Reason: I found a better solution.
bardonw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:41 PM.


Advertisement
Log in to turn off these ads.