Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting numbers in english 10

Status
Not open for further replies.

BabyJeffy

Programmer
Sep 10, 2003
4,189
GB
The way we count numbers in English appears quite strange (I am sure) to those who don't speak it natively. When we refer to dates we often add "st", "rd", "nd" or "th" to the end of any numbers. Example: 10th February, 23rd June

I remember one solution I used for solving this "problem" early in my career. There can be up to 31 days in a month, so I created an array and populated it with the correct endings:
Code:
var numberWords = ['X','st','nd','rd','th','th','th','th','th','th','th','th','th','th',
'th','th','th','th','th','th','th','st','nd','rd','th','th','th','th','th','th','th','st'];
And I would then get the correct ending by calling the index of the array with the number directly:
Code:
function getNumberExt(theNumber) {
  return (theNumber+numberWords[theNumber]);
}
alert(getNumberExt(23)); // alerts 23rd
My example contains no attempt at error checking (and this is certainly not appropriate for production quality code).

I've used much the same code server-side as well as client-side and it's done the job well. I am interested in how others have achieved a similar result.

Does anyone have any significantly different mechanisms to do this? Want to share?

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Doh, I only tested: 0-4,11-14,21,100-104, 111-114, and 121
 
Some hints:

Why testing for high numbers? Does anyone know about a month with more than 31 days? I hope it's my holidays month.

Isn't there any Date object or something similar that can do that itself?

Cheers,
Dian
 
Sure, Dian... you are of course right regarding the date example - there is no need to look after 31.

The thread isn't looking at a specific problem so much as allowing us to flex our brains a little and come up with alternatives to the method I used a while back. It's not for a specific project or anything.

It's the journey that makes it fun [smile]

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
I was just curious. I think I've hyperengineered my mind, I'm all day thinking on not wasting resources.

Anyway, and appart from the funny part, isn't there a simple way to format a date without making the brain sweat?

Cheers,
Dian
 
No simple way to get the 'st', 'rd', 'nd' and 'th' correct on the numbers. Yyou are right that javascript has some decent date functions... just no core code to do that kind of thing.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
What does the Date.toLocaleDateString() return in English?

Cheers,
Dian
 
Dian,

On my Windows OS which has English as the default language, it returns:

"Thursday, November 24, 2005"

However, I suspect it wouldn't return that for an OS set to any non-English languge.

Dan


[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
In Spanish, it retuns the correct spanish format, I just got a little confused with the idea of JS being not locale-dependant, I always thought you could handle it.

But that doesn't return the "th" thingie, anyway. You, english speakers, should simplify the way you write dates :p

Cheers,
Dian
 
A deceptively un-simple little challenge. I thought it would be easy enough to make an "English major" optimization -- write a JavaScript solution that doesn't require an enormous amount of decoding to extract the algorithm. Self-documenting, sort of.

The English language rules for formulating the ordinal number suffix are quite simple. So simple I'd never thought about it before. As a native speaker I can automatically supply this suffix for any Integer based on, it turns out, two rules. One: if the penultimate digit of the Integer is a "1" then the suffix is "th". Two: in all other cases apply the "1","2","3" exceptions, default to "th". As a JavaScript function accepting the last two digits of the Integer as arguments --
Code:
var aSuffix = new Array("th","st","nd","rd");
function _suffix( y,z ){
  var i = ( y == 1 ) ? 0 : z;
  i     = ( aSuffix[ i ] ) ? i : 0;
  return ( aSuffix[ i ] );
}
To use this function we must separately extract the last two digits of the Integer. Logically the last digit is (I % 10), the penultimate is ((I / 10) % 10). IE's JavaScript doesn't quite agree unless explicitly told to use just the integer part of (I / 10) --
Code:
function calc_OrdinalSuffix( a ) {
  return _suffix( parseInt(a/10)%10,a%10 );
}
Works fine for all positive values of Integer but returns "th" for all negative values. IE says that (-1 % 10) is -1 and they're probably right. To compensate we must make sure the calculation function gets a positive number --
Code:
function OrdinalSuffix( n ) {
  return n + calc_OrdinalSuffix( Math.abs(n) );
}
Put in a couple of comments and maybe we won't have to work so hard to understand this solution next time --
Code:
// append the English language ordinal suffix for Integer n
//   "st" if n ends in 1, "nd" for 2, "rd" for 3, else "th"
//    EXCEPTION 11th, 12th, 13th
//
// convert negatives into positives
function OrdinalSuffix( n ) {
  return n + calc_OrdinalSuffix( Math.abs(n) );
}

// extract the 2nd last, last digits of n
function calc_OrdinalSuffix( a ) {
  return _suffix( parseInt(a/10)%10,a%10 );
}

// all teens are "th"
// otherwise apply 1,2,3 rule
var aSuffix = new Array("th","st","nd","rd");
function _suffix( y,z ){
  var i = ( y == 1 ) ? 0 : z;
  i     = ( aSuffix[ i ] ) ? i : 0;
  return ( aSuffix[ i ] );
}
Another example of a hard we have to work to come up with a simple solution to a simple problem.
 
Ok, for a challenge I did it with no booleans and in vbScript:
thread333-1167544

:)

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top