Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 3 of 3 FirstFirst 123
Results 31 to 45 of 45
  1. #31
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 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]; 
    }

  2. #32
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by RoyW View Post
    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)]; 

    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 04:09 PM.

  3. #33
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 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.

  4. #34
    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]

  5. #35
    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 05:34 PM.

  6. #36
    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?!
    Last edited by AndrewGSW; 10-10-2012 at 09:58 PM.
    "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

  7. #37
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Code:
    <script type="text/javascript">
    
    var D = 12;
    var XX = "thstndrd".substr((D % 10) * 2, 2) || "th";
    alert (XX);  // nd!!!
    
    </script>

    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.

  8. #38
    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 View Post
    Code:
    <script type="text/javascript">
    
    var D = 12;
    var XX = "thstndrd".substr((D % 10) * 2, 2) || "th";
    alert (XX);  // nd!!!
    
    </script>
    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.
    Validate your HTML and CSS

  9. #39
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 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. #40
    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.
    Last edited by AndrewGSW; 10-11-2012 at 12:01 AM.
    "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

  11. #41
    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

  12. #42
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by AndrewGSW View Post
    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";
    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!
    Last edited by Philip M; 10-11-2012 at 08:07 AM.

    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.

  13. #43
    New Coder
    Join Date
    Dec 2009
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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.

  14. #44
    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

  15. #45
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,013
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by bardonw View Post
    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.


 
Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •