Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Add ST/ND/RD/TH to DAY in DATE

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-26-2010, 03:55 PM PM User | #31 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,478 Thanks: 200 Thanked 2,470 Times in 2,448 Posts I still prefer Code: ```function numsuffix(n) { return n + ["th","st","nd","rd"][!(n%10>3||Math.floor(n%100/10)==1)*n%10]; }```
03-26-2010, 05:02 PM   PM User | #32
bardonw
New Coder

Join Date: Dec 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by RoyW 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 n + ["th","st","nd","rd"][(!( ((n%10) >3) || (Math.floor(n%100/10)==1)) ) * (n%10)]; }  ```
I must admit, this is some impressive code! After seeing this I decided to optimize it just a bit by storing the array of suffixes privately. After doing that and modifying it to have the same footprint as my generic function, I ended up with the following code:
Code:
```var num_abbrev_str = (function()
{
var z = ["th","st","nd","rd"];
return function(intNum, includeNumber)
{
return (includeNumber ? intNum : "") + z[!((intNum = Math.abs(intNum))
% 10 > 3 || Math.floor(intNum % 100 / 10) == 1) * intNum % 10];
};
})();```
The plus side is, the function works very well. The only disadvantage is, on Internet Explorer and Firefox, this function takes longer to run, for a large number of integers, than the code below does. On the other hand, I doubt that too many people will be running this type of function for 100,000+ numbers.
Code:
```function num_abbrev_str(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");
}```
Still, your code is most impressive.

Last edited by bardonw; 03-26-2010 at 05:09 PM..

 03-26-2010, 05:42 PM PM User | #33 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,478 Thanks: 200 Thanked 2,470 Times in 2,448 Posts In this forum it is often said that there are several good ways to skin a cat, but it is also said that re-inventing the wheel (but making it oval this time) does not add very much.
 03-27-2010, 03:54 AM PM User | #34 RoyW Regular Coder   Join Date: Jun 2002 Location: Atlanta, GA. Posts: 313 Thanks: 0 Thanked 0 Times in 0 Posts Looks like I still have email notifications on for this thread. A 7 year old thread! That must be a record __________________ The answer does not come from thinking outside the box, it comes from realizing the truth :- "There Is No Box". [JavaScript Gadgets'n'Gizmos][JavaScript-FX]
 10-10-2012, 06:05 PM PM User | #35 bardonw New Coder   Join Date: Dec 2009 Posts: 11 Thanks: 1 Thanked 0 Times in 0 Posts After looking at someone else's implementation, I improved mine and created the smallest version that I could think of with the first parameter being the number and the second being the boolean indicating whether or not to show the number. I also used a closure in order to prevent the array from being recreated every time the function ran (this improves the speed): Code: `(function(b){Number.getOrdinalFor=function(a,c){return(c?a:"")+(b[((a=Math.abs(a%100))-20)%10]||b[a]||"th")}})([,"st","nd","rd"]);` This function is only 130 characters long and will work for all integers. I also came up with a pretty cool example of how to use this for a string full of numbers: Code: ```var str = "1, 2, 3, 4.5, 6, and 7 are all numbers."; alert(str.replace(/(\d+)/g, Number.getOrdinalFor));``` I wrote a blog post about this function definition here: http://gotochriswest.com/blog/2012/0...getordinalfor/ Let me know if you can write the equivalent function in an even shorter way while also making sure that something like an array doesn't have to be generated every time the function is called. That last requirement is what makes my function a little longer than it seems it needs to be. Last edited by bardonw; 10-10-2012 at 06:34 PM..
 10-10-2012, 10:50 PM PM User | #36 AndrewGSW Senior Coder   Join Date: Apr 2011 Location: London, England Posts: 2,120 Thanks: 15 Thanked 354 Times in 353 Posts You could use a string rather than an array, incorporating: XX = "thstndrd".substr((D % 10) * 2, 2) || "th"; It may not be shorter but avoids "Index out of range" or "undefined". Oops! This is not quite right?! __________________ "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene. Validate your HTML and CSS Last edited by AndrewGSW; 10-10-2012 at 10:58 PM..
 10-10-2012, 11:02 PM PM User | #37 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,478 Thanks: 200 Thanked 2,470 Times in 2,448 Posts Code: `````` __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.
10-10-2012, 11:04 PM   PM User | #38
AndrewGSW
Senior Coder

Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
Quote:
 Originally Posted by Philip M Code: ``````
Yes, shame though - it looked good! Perhaps we can change the suffixes to match my formula .
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.

 10-10-2012, 11:06 PM PM User | #39 WolfShade Regular Coder   Join Date: Apr 2012 Location: St. Louis, MO, USA Posts: 966 Thanks: 7 Thanked 100 Times in 100 Posts __________________ ^_^ If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link. * The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
 10-10-2012, 11:38 PM PM User | #40 AndrewGSW Senior Coder   Join Date: Apr 2011 Location: London, England Posts: 2,120 Thanks: 15 Thanked 354 Times in 353 Posts Code: `XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th";` I'm considering dates though (from the title of this post) so haven't gone beyond 31 Added: Yes, works beyond 31. __________________ "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene. Validate your HTML and CSS Last edited by AndrewGSW; 10-11-2012 at 01:01 AM..
 10-11-2012, 04:09 AM PM User | #41 AndrewGSW Senior Coder   Join Date: Apr 2011 Location: London, England Posts: 2,120 Thanks: 15 Thanked 354 Times in 353 Posts Code: `XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";` .. works with non-date numbers. __________________ "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene. Validate your HTML and CSS
10-11-2012, 09:03 AM   PM User | #42
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,478
Thanks: 200
Thanked 2,470 Times in 2,448 Posts
Quote:
 Originally Posted by AndrewGSW Code: `XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";` .. works with non-date numbers.
No it doesn't!

Code:
```var D = 112;
var XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th";

Code:
```var D = 112;
var XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th";
See Post # 31 - that does work!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-11-2012 at 09:07 AM..

10-12-2012, 09:49 PM   PM User | #43
bardonw
New Coder

Join Date: Dec 2009
Posts: 11
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by Philip M No it doesn't! Code: ```var D = 112; var XX = "thstndrd".substr((D % 10) * (D < 11 || D > 13) * 2, 2) || "th"; alert (XX); // nd!!!``` Code: ```var D = 112; var XX = "thstndrd".substr((D % 10) * !(D % 14 > 9) * 2, 2) || "th"; alert (XX); // nd!!!``` See Post # 31 - that does work!
I think that is a good version and I definitely spent some time looking at how to make it work a bit differently. The only problem I have is the fact that in that version, the array of ordinals is created every time the function is run. That means that the function will run slower if run on many thousands of numbers. Still, for most cases, that function will do. I just know that people like John Resig or Douglas Crockford would not like me endorsing that as the best solution.

 10-13-2012, 03:55 AM PM User | #44 AndrewGSW Senior Coder   Join Date: Apr 2011 Location: London, England Posts: 2,120 Thanks: 15 Thanked 354 Times in 353 Posts I suppose.. Code: `var XX = "thstndrd".substr((D % 10) * (D % 100 < 11 || D % 100 > 13) * 2, 2) || "th";` but my interest was really in working with days (from the OP title). __________________ "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene. Validate your HTML and CSS
10-13-2012, 08:49 AM   PM User | #45
Philip M
Supreme Master coder!

Join Date: Jun 2002
Location: London, England
Posts: 17,478
Thanks: 200
Thanked 2,470 Times in 2,448 Posts
Quote:
 Originally Posted by bardonw The only problem I have is the fact that in that version, the array of ordinals is created every time the function is run. That means that the function will run slower if run on many thousands of numbers. Still, for most cases, that function will do. I just know that people like John Resig or Douglas Crockford would not like me endorsing that as the best solution.
I would doubt if there was any perceptible slowness (milliseconds perhaps) even if run on "many thousand of numbers". And where in reality is that required? Does the need to generate 1st to 123456th ever really arise?
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 05:26 PM.