18

Is there any inbuilt js/jquery function that converts 1 to first, 2 to second, 3 to third... etc.?

ex:

Num2Str(1); //returns first;
Num2str(2); //returns second;

I dont want to write a function for 100 numbers. Please help.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
EvilDevil
  • 680
  • 1
  • 7
  • 18
  • 4
    umm, show code for starters.., also we do not do your homework – Vogel612 Dec 06 '13 at 14:05
  • 3
    **No**, there is no inbuilt function. – Bergi Dec 06 '13 at 14:06
  • 2
    The answer is no. but you can write your own function which will be the one of the most useless functions of all time – FreshPro Dec 06 '13 at 14:09
  • 3
    @FreshPro i have to be disagree concerning usefulness of this kind of function – A. Wolff Dec 06 '13 at 14:11
  • Take a [look at this](http://javascript.about.com/library/bltoword.htm), I think this could be what your looking for. Although it doesn't use first, second etc. but one, two, three etc. – AfromanJ Dec 06 '13 at 14:11
  • @Vogel612 I did not start writing the code yet. I am searching for a built in function to avoid writing it. Looks like there is none available. Tibos and Vogel612: Thanks for the pointers. They are very helpful! – EvilDevil Dec 06 '13 at 15:55
  • I did not know the term ordinals. Hence, couldn't find the relevant thread :( Sorry for the duplicate post. – EvilDevil Dec 06 '13 at 15:58

2 Answers2

42

There is no inbuilt function for it.

I did write one for up to 99:

var special = ['zeroth','first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth', 'tenth', 'eleventh', 'twelfth', 'thirteenth', 'fourteenth', 'fifteenth', 'sixteenth', 'seventeenth', 'eighteenth', 'nineteenth'];
var deca = ['twent', 'thirt', 'fort', 'fift', 'sixt', 'sevent', 'eight', 'ninet'];

function stringifyNumber(n) {
  if (n < 20) return special[n];
  if (n%10 === 0) return deca[Math.floor(n/10)-2] + 'ieth';
  return deca[Math.floor(n/10)-2] + 'y-' + special[n%10];
}

// TEST LOOP SHOWING RESULTS
for (var i=0; i<100; i++) console.log(stringifyNumber(i));

DEMO: http://jsbin.com/AqetiNOt/1/edit

Fuzz
  • 906
  • 1
  • 12
  • 24
Tibos
  • 27,507
  • 4
  • 50
  • 64
4

You could create a numberbuilder:

You will need to create a foolproof way to convert the single digits by power to a string.

1234 -->1(one)*10^3(thousand)+2(two)*10^2(hundred)+3(three)10(ten)+4(four)(one)
==> one thousand two hundred th irty four th

123456 --> one hundred tw enty three thousand four hundred fi fty six th

if you are wondering about the notation: I tried to split this up in the single decision steps you need to make

the rules for building repeat every three digits. The rest is up to you.

Oh and before I forget: there is only "3" exceptions to the th-rule. one, two and three.

Vogel612
  • 5,620
  • 5
  • 48
  • 73