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:
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.
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.
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]
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):
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));
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.
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 09:58 PM..
<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
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".
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 12:01 AM..
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
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.
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
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.