I've successfully built a numbers to words converter for 1-199, for a foreign language that uses a vigesimal number system, counting in twenties. I'd now like to add 200-999.
Here's what I have so far. HTML:
<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"> </span></td></tr>
</table>
Javascript:
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"
function getnum(s){
var n = parseInt(s);
if( 1 <= n && n <= 20 ){ alert(units[n]); }
if( 21 <= n && n <= 39 ){
var r = Math.floor(n - 20);
r && (alert (units[r] + ' warn ' + units[20]));
}
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] + ' ha ' + units[q] + ' ' + units[20]));
}
if( 100 <= n && n <= 100 ){ alert('cans'); }
if( 101 <= n && n <= 119 ){
var r = Math.floor(n - 100);
r && (alert ('cans ha ' + units[r]));
}
}
The trouble with 200-999 is that I think I need to specify separate rules within each hundred - for example within the 200s, 200-220 is "two hundred and x", but 221-299 is "two hundred one and twenty" to "two hundred nineteen and four twenty", using the same vigesimal system as 21-99. How can I expand the code I already have to allow me to add 200-999?
These are the words for the hundreds - it needs to be done this way because of the exception "tri hans" (unless there's a better way to account for it)
var hundreds = new Array(11)
hundreds[0]=""
hundreds[1]="cans"
hundreds[2]="dew cans"
hundreds[3]="tri hans"
hundreds[4]="pajar cans"
hundreds[5]="pemp cans"
hundreds[6]="whegh cans"
hundreds[7]="seyth cans"
hundreds[8]="eth cans"
hundreds[9]="naw cans"
hundreds[10]="mil"
Thanks