Author Topic: RESOURCE: Number To Word functions  (Read 1919 times)

Two functions which allow changing an integer (389285) into a human-readable string (three hundred eighty-nine thousand, two hundred eighty-five).

Code: (NumToWord.cs) [Select]
function NumToWord(%num)
{
if(%num $= "-")
return "Invalid Number";
for(%i=0;%i<strLen(%num);%i++)
{
%char = getSubStr(%num, %i, 1);
if(%char $= "-" && %i != 0)
return "Invalid Number";
else if(strPos("0123456789", %char) == -1)
return "Invalid Number";
}
if(%num == 0)
return "zero";
if(%num < 0)
{
%word = "negative ";
%num = mAbs(%num);
}
%threeWordList = " thousand million billion trillion quadrillion quintillion";
%digitList = " one two three four five six seven eight nine";
%teensList = " eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen";
%tensList = " ten twenty thirty forty fifty sixty seventy eighty ninety";
while((%len = strLen(%num)) > 0)
{
%subWord = "";
%subLen = (%len + 3) - mFloor(%len / 3) * 3;
%val = getSubStr(%num, 0, %subLen);
if(%len > 3)
%num = getSubStr(%num, %subLen, %len - %subLen);
else
%num = "";
while(strLen(%val) < 3)
%val = "0" @ %val;
for(%i=0;%i<3;%i++)
%sub[%i] = getSubStr(%val, %i, 1);
if(%sub0 != 0)
%subword = getWord(%digitList, %sub0)@" hundred";
if(%sub1 == 1 && %sub2 != 0)
%subword = %subword SPC getWord(%teensList, %sub2);
else if(%sub1 != 0)
%subword = %subword SPC getWord(%tensList, %sub1)@(%sub2!=0?"-"@getWord(%digitList,%sub2):"");
else if(%sub2 != 0)
%subword = %subword SPC getWord(%digitList, %sub2);
%subword = %subword SPC getWord(%threeWordList, strLen(%num) / 3);
if(%subword !$= "")
%word = %word@(%word!$=""?", ":"")@trim(%subword);
}
return trim(%word);
}

function OrdinalNumToWord(%num)
{
if(%num $= "-")
return "Invalid Number";
for(%i=0;%i<strLen(%num);%i++)
{
%char = getSubStr(%num, %i, 1);
if(%char $= "-" && %i != 0)
return "Invalid Number";
else if(strPos("0123456789", %char) == -1)
return "Invalid Number";
}
if(%num == 0)
return "zero";
if(%num < 0)
{
%word = "negative ";
%num = mAbs(%num);
}
%threeWordList = " thousand million billion trillion quadrillion quintillion";
%digitList = " one two three four five six seven eight nine";
%teensList = " eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen";
%tensList = " ten twenty thirty forty fifty sixty seventy eighty ninety";
%ordDigitList = " first second third fourth fifth sixth seventh eigth ninth";
%ordTeensList = " eleventh twelfth thirteenth fourteenth fifteenth sixteenth seventeenth eighteenth nineteenth";
while((%len = strLen(%num)) > 0)
{
%subword = "";
if(%len > 3)
{
%val = getSubStr(%num, %len - 3, 3);
%num = getSubStr(%num, 0, %len - 3);
}
else
{
%val = %num;
%num = "";
}
while(strLen(%val) < 3)
%val = "0" @ %val;
for(%i=0;%i<3;%i++)
%sub[%i] = getSubStr(%val, %i, 1);
if(%sub0 != 0)
%subword = getWord(%digitList, %sub0)@" hundred";
if(%sub1 == 1 && %sub2 != 0)
%subword = %subword SPC getWord(%ord?%teensList:%ordTeensList, %sub2);
else if(%sub1 != 0)
if(%sub2 != 0)
{
%subword = %subword SPC getWord(%tensList, %sub1)@"-"@getWord(%ord?%digitList:%ordDigitList, %sub2);
%ord = 1;
}
else
{
%subword = %subword SPC strReplace(getWord(%tensList, %sub1), "y", %ord?"y":"ieth");
%ord = 1;
}
else if(%sub2 != 0)
{
%subword = %subword SPC getWord(%ord?%digitList:%ordDigitList, %sub2);
%ord = 1;
}
else if(%sub0 != 0 && !%ord)
{
%subword = %subword@"th";
%ord = 1;
}
%subword = %subword SPC getWord(%threeWordList, %tick);
if(%subword !$= "")
%word = trim(%subword)@(%word!$=""?", ":"")@%word;
%tick++;
}
return trim(%word);
}


Function 1: NumToWord(%number);
Purpose: Returns a number as a human-friendly string.
Function Speed: Tested with 1234567, ran function 1000000 times. Function ran at an average of 0.098 MS.

Function 2: OrdinalNumToWord(%number);
Purpose: Returns a number as a human-friendly ordinal string. (First, second, third, fourth, so on.)
Function Speed: Tested with 1234567, ran function 1000000 times. Function ran at an average of 0.120 MS.
« Last Edit: October 03, 2012, 12:28:06 PM by Xalos »

The benchmarks are nifty, what setup did you run them on?

Did you have the function output the results somehow during your one million iterations? Eg using echo()? That'll slow it down significantly.

Also, if you would, prepend "RESOURCE:" to your title, please
« Last Edit: October 03, 2012, 12:27:39 PM by Lugnut »

The benchmarks are nifty, what setup did you run them on?

Did you have the function output the results somehow during your one million iterations? Eg using echo()? That'll slow it down significantly.

Also, if you would, prepend "RESOURCE:" to your title, please

Kay. Also, no; I didn't use echo - if I was testing how inefficient it is to update the console a million times I would do it directly. (Also, the answer is "very".)

I would've made this myself but I don't know how to generate the names for the prefixes, like quadrillion, septillion, quadroseptillion, etc.