Jump to content

From javascript to AutoIt, number to words?


Recommended Posts

Hello,

I have an old java script code that I want to convert to AutoIt, however I don't know how to do this. I have tried many times, but failed. So I'm posting the code here, and I hope that someone can take the time to just port it to AutoIt and send it back to me, that would be highly appreciated. It is a script that will convert numbers to words. For example, 345 would return Three Hundred Forty Five. I am attaching the .js file in this message.

Thank's in advance,

Philip Bennefall

I am not sure if the file got properly attached, so I am pasting the code here as well.

---Code start---

aUnits=["","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"]

aTeens=["","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","Nineteen"]

aTens =["","Ten","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty","Ninety"]

function getunits(xValue)

{

return (aUnits[xValue])

}

function gethund(xValue)

{

return (aUnits[xValue] != '') ? (aUnits[xValue]+' Hundred') : (aUnits[xValue])

}

function teens(xValue)

{

return (aTeens[xValue])

}

function tens(xValue)

{

return (aTens[xValue])

}

function ConvertNumberToWords(nAmount)

{

var sAmount=nAmount.toString();

if (sAmount.indexOf(".") == -1) sAmount+='.00'

var sMax = (12 - sAmount.length)

var cStringAmount='';

var sNumberInWords ='';

for (c=1; c <=sMax; c++)

{

cStringAmount += '0'

}

sAmount=cStringAmount+sAmount;

cStringAmount=sAmount.substring(0,9);

var str1= sAmount.substring(0,3);

var str2= sAmount.substring(3,6);

var str3= sAmount.substring(6,9);

var str4= sAmount.substring(10,12)

if (cStringAmount.length >= 7)

{

sWords = NumberInWords(str1);

if (sWords.length >= 3) sNumberInWords += sWords +" Million ";

cStringAmount=cStringAmount.substring(3,9)

}

if (cStringAmount.length >= 4)

{

sWords = NumberInWords(str2)

if (sWords.length >= 3) sNumberInWords += sWords +" Thousand ";

cStringAmount = cStringAmount.substring(6,9)

}

if (cStringAmount.length <= 3)

{

sWords = NumberInWords(str3);

sNumberInWords += sWords+' '

}

var sTens=gettens(str4);

sTens=((sTens == " ") ? 'Zero' : sTens)

sNumberInWords += ((sTens <= 0) ? '' : '');

//sNumberInWords+='and '+str4+'/100'

return (sNumberInWords)

}

function NumberInWords(cString)

{

xhund =gethund(cString.substring(0,1))

xtens =gettens(cString.substring(1,3))

xwords=xhund

xwords+= ((xtens.length >= 3) ? ' ' : '')+xtens

return (xwords);

}

function gettens(xValue)

{

xval_ =xValue.substring(0,1);

_xval =xValue.substring(1,2);

if (xval_ == '1' && _xval != '0')

{

xnum= teens(_xval)

}

if (xval_ == '1' && _xval == '0') xnum="Ten"

if (xval_ != '1' && (_xval != '0' || _xval == '0'))

{

xnum= tens(xval_)+" "+getunits(_xval);

}

return (xnum)

}

---Code end---

Edited by Philip Bennefall
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...