Javascript numbers to words - vigesimal

2019-05-13 01:04发布

I'd like to create a numbers to words converter such as this one http://gwydir.demon.co.uk/jo/numbers/words/intro.htm

However I'm doing it for a foreign language that uses a vigesimal number system - counting in twenties. I'd like to start out with 1-199.

1-20 is easy, but after that it changes. 20-39 follows the pattern of 'one on twenty' (21), 'two on twenty' (22), up to 'eighteen on twenty' (38), 'nineteen on twenty' (39).

40-99 and 120-199 is different still, since it counts in scores: 'one and two twenty' (41), 'two and two twenty' (42), 'one and three twenty' (61), ten and three twenty (70), eleven and three twenty (71), 'one and four twenty' (81), 'six twenty' (120), 'one and six twenty' (121), 'nine twenty' (180), 'one and nine twenty' (181), 'nineteen and nine twenty' (199)

100-119 is similar to English: hundred and one (101), hundred and two (102), hundred and eleven (111), hundred and nineteen (119)

This is the html I'm using for the input form:

<table align=center>
<tr><td>
<form action="#" name="conv">
Enter number:
<input type="text" name="numb" size="15">
<input type="button" value="Show number" onclick="getnum(document.conv.numb.value)">
</form>
</td></tr>
<tr><td align=center><span id="outp">&nbsp;</span></td></tr>
</TABLE>

And this is the Javascript I have for the numbers up to twenty:

var units = new Array(21)
units[0]=""
units[1]="onen"
units[2]="dew"
units[3]="tri"
units[4]="pajar"
units[5]="pemp"
units[6]="whegh"
units[7]="seyth"
units[8]="eth"
units[9]="naw"
units[10]="deg"
units[11]="udnek"
units[12]="dowdhek"
units[13]="tredhek"
units[14]="peswardhek"
units[15]="pemthek"
units[16]="whetek"
units[17]="seytek"
units[18]="etek"
units[19]="nownsek"
units[20]="ugens"

My question is, how do I start creating the code that will allow me to convert numbers into the correct words? Can anyone point me to some examples that will help? I've seen examples of it done for English but because the number systems are so different I can't see how to convert it to a vigesimal system.

Thanks

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-13 01:30

The following function should give you an idea how to approach this:

function getnum(s){
    var n = parseInt(s);

    if( 1 <= n && n <= 20 ){ alert(units[n]); }

    if( (40 <= n && n <= 99) || (120 <= n && n <= 199) ){
      var q = Math.floor(n / 20);
      var r = n % 20;

      !r && (alert (units[q] + ' ' + units[20]));

      r && (alert (units[r] + ' and ' + units[q] + ' ' + units[20]));    
    }    
}

Now you should be able to figure out the conversion for 100 <= number <= 119. Also similar for 21 <= number <= 39.

查看更多
对你真心纯属浪费
3楼-- · 2019-05-13 01:30

Let's work through an example: Consider the number x.

First figure out how many 100's are in the number. We do that like this: x/100 since integer division truncates off any remainder. So that's our first segment: units[x/100] + " hundred"

Now let's do the remainder, given by y=x%100. There are y/20 twenties. And y%20 remaining. So the rest of the string is units[y%20] + " and " + units[y/20] + " " units[20].

This isn't 100% perfect just yet since it doesn't correctly handle numbers with no remainders like 640. But those are just a few special casees which I'm sure you can figure out without any trouble.

查看更多
登录 后发表回答