Spelling of ordinal numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|zkl}}: added code)
m (→‎{{header|zkl}}: change output format)
Line 139: Line 139:
<lang zkl>testNs:=L(1,2,3,4,5,11,65,100,101,272,23456,8007006005004003,
<lang zkl>testNs:=L(1,2,3,4,5,11,65,100,101,272,23456,8007006005004003,
123,00123.0,1.23e2,);
123,00123.0,1.23e2,);
foreach n in (testNs){ println("%16d --> %s".fmt(n,nth(n))); }</lang>
foreach n in (testNs){
if(n.isType(Float)) println("%16.2f --> %s".fmt(n,nth(n)));
else println("%16d --> %s".fmt(n,nth(n)));
}</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 155: Line 158:
8007006005004003 --> eight quadrillion seven trillion six billion five million four thousand third
8007006005004003 --> eight quadrillion seven trillion six billion five million four thousand third
123 --> one hundred twenty-third
123 --> one hundred twenty-third
123 --> one hundred twenty-third
123.00 --> one hundred twenty-third
123 --> one hundred twenty-third
123.00 --> one hundred twenty-third
</pre>
</pre>

Revision as of 01:44, 9 September 2017

Spelling of ordinal numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Ordinal numbers   (as used in this Rosetta Code task),   are numbers that describe the   position   of something in a list.

It is this context that ordinal numbers will be used, using an English-spelled name of an ordinal number.


The ordinal numbers are   (at least, one form of them):

  1st  2nd  3rd  4th  5th  6th  7th  ···  99th  100th  ···  1000000000th  ···  etc

sometimes expressed as:

  1st  2nd  3rd  4th  5th  6th  7th  ···  99th  100th  ···  1000000000th  ···


For this task, the following (English-spelled form) will be used:

  first second third fourth fifth sixth seventh ninety-nineth one hundredth one billionth


Furthermore, the American version of numbers will be used here   (as opposed to the British).

2,000,000,000   is two billion,   not   two milliard.


Task

Write a driver and a function (subroutine/routine ···) that returns the English-spelled ordinal version of a specified number   (a positive integer).

Optionally, try to support as many forms of an integer that can be expressed:   123   00123.0   1.23e2   all are forms of the same integer.

Show all output here.


Test cases

Use (at least) the test cases of:

  1  2  3  4  5  11  65  100  101  272  23456  8007006005004003


Related tasks




Perl 6

Works with: Rakudo version 2017.08

This would be pretty simple to implement from scratch; it would be straightforward to do a minor modification of the Number names task code. Much simpler to just use the Lingua::EN::Numbers::Cardinal module from the Perl 6 ecosystem though. It easily handles ordinal numbers even though that is not its primary focus.

We need to be slightly careful of terminology. In Perl 6, 123, 00123.0, & 1.23e2 are not all integers. They are respectively an Int (integer), a Rat (rational number) and a Num (floating point number). For this task it doesn't much matter as the ordinal routine coerces its argument to an Int, but to Perl 6 they are different things. We can further abuse allomorphic types for some somewhat non-intuitive results as well.

It is not really clear what is meant by "Write a driver and a function...". Well, the function part is clear enough; driver not so much. Perhaps this will suffice.

<lang perl6>use Lingua::EN::Numbers::Cardinal;

printf( "\%16s : %s\n", $_, ordinal($_) ) for

  1. Required tests

|<1 2 3 4 5 11 65 100 101 272 23456 8007006005004003>,

  1. Optional tests

|<123 00123.0 1.23e2 123+0i 0b1111011 0o173 0x7B 861/7>;</lang>

Output:
               1 : first
               2 : second
               3 : third
               4 : fourth
               5 : fifth
              11 : eleventh
              65 : sixty-fifth
             100 : one hundredth
             101 : one hundred first
             272 : two hundred seventy-second
           23456 : twenty-three thousand, four hundred fifty-sixth
8007006005004003 : eight quadrillion, seven trillion, six billion, five million, four thousand third
             123 : one hundred twenty-third
         00123.0 : one hundred twenty-third
          1.23e2 : one hundred twenty-third
          123+0i : one hundred twenty-third
       0b1111011 : one hundred twenty-third
           0o173 : one hundred twenty-third
            0x7B : one hundred twenty-third
           861/7 : one hundred twenty-third

REXX

<lang REXX>/*REXX programs spells out ordinal numbers (in English, using the American system). */ numeric digits 3000 /*just in case the user uses gihugic #s*/ parse arg n /*obtain optional arguments from the CL*/

if n= | n="," then n= 1 2 3 4 5 11 65 100 101 272 23456 8007006005004003

pgmOpts= 'ordinal quiet' /*define options needed for $SPELL#.REX*/


    do j=1  for words(n)                        /*process each of the specified numbers*/
    x=word(n, j)                                /*obtain a number from the input list. */
    os=$spell#(x  pgmOpts)                      /*invoke REXX routine to spell ordinal#*/
    say right(x, max(20, length(x) ) )      ' spelled ordinal number ───► '      os
    end   /*j*/</lang>
output   when using the default inputs:
                   1  spelled ordinal number ───►  first
                   2  spelled ordinal number ───►  second
                   3  spelled ordinal number ───►  third
                   4  spelled ordinal number ───►  fourth
                   5  spelled ordinal number ───►  fifth
                  11  spelled ordinal number ───►  eleventh
                  65  spelled ordinal number ───►  sixty-fifth
                 100  spelled ordinal number ───►  one hundredth
                 101  spelled ordinal number ───►  one hundred first
                 272  spelled ordinal number ───►  two hundred seventy-second
               23456  spelled ordinal number ───►  twenty-three thousand four hundred fifty-sixth
    8007006005004003  spelled ordinal number ───►  eight quadrillion seven trillion six billion five million four thousand third

The   $SPELL#.REX   routine can be found here   ───►   $SPELL#.REX.

zkl

<lang zkl>fcn nth(n,th=True){

  var [const]
    nmsth=T("","first","second","third","fourth","fifth","sixth","seventh","eighth","ninth"),
    nms1=T("","one","two","three","four","five","six","seven","eight","nine"),
    nms10=T("ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),
    nms20=T("twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"),
    nms1000=T("thousand","million","billion","trillion","quadrillion"); // 3,6,9,12,15
  if(n<10)        th and nmsth[n] or nms1[n];
  else if(n<20)   String(nms10[n-20],th and "th" or "");
  else if(n<100)  String(nms20[n/10-2],dash(n%10,"-",th));
  else if(n<1000) String(nms1[n/100]," hundred",dash(n%100," ",th));
  else{
     n=n.toInt();   // yuck, only here to handle floats, 1.23-->"first"
     ds:=(n.numDigits()-1)/3*3; // 1e3->3, 1e4-->3, 1e5-->3, 1e6-->6, 1e7-->6
     z:=(10).pow(ds);  // 1234-->1000, 12345-->10000
     thou:=ds/3 - 1;	// 1000-->0, 10000-->0, 2,000,000-->1
     nnn,ys := n/z, n%z;
     String((if(nnn<10) nms1[nnn] else nth(nnn,False)),

" ",nms1000[thou], if(ys==0) "th" else String(" ",nth(ys)));

  }

} fcn dash(n,d,th){ if(n) String(d,nth(n,th)) else (th and "th" or "") }</lang> <lang zkl>testNs:=L(1,2,3,4,5,11,65,100,101,272,23456,8007006005004003,

         123,00123.0,1.23e2,);

foreach n in (testNs){

  if(n.isType(Float)) println("%16.2f --> %s".fmt(n,nth(n)));
  else		       println("%16d --> %s".fmt(n,nth(n)));

}</lang>

Output:
               1 --> first
               2 --> second
               3 --> third
               4 --> fourth
               5 --> fifth
              11 --> eleventh
              65 --> sixty-fifth
             100 --> one hundredth
             101 --> one hundred first
             272 --> two hundred seventy-second
           23456 --> twenty-three thousand four hundred fifty-sixth
8007006005004003 --> eight quadrillion seven trillion six billion five million four thousand third
             123 --> one hundred twenty-third
          123.00 --> one hundred twenty-third
          123.00 --> one hundred twenty-third