Monday, August 4, 2008

Numbers to words in Flash

It is sometimes necessary to convert a given number into its equivalent value in words.

At first this might appear to be a daunting and complex task but, fortunately, the method for formulating spoken numbers in English can be broken down into a set of simple rules. These rules are then applicable for any number, regardless of its size:

  1. if the number value is zero then the number in words is 'zero' and no other rules apply.
  2. all numbers can be split into groups of three digits starting from the right-hand side. Each group of three digits can then be processed individually to obtain their hundreds, tens and unit word equivalents.
  3. if the hundreds portion of a three-digit group is not zero, the number of hundreds is added as a word. If the three-digit group is exactly divisible by one hundred, the text hundred is appended. If not, the text hundred and is appended, for example three hundred or one hundred and forty six.
  4. if the tens section of a three-digit group is two or higher, the appropriate -ty word (twenty, thirty, etc.) is added to the text and followed by the name of any non-zero third digit. If the tens and the units are both zero, then no text is added. For all other values, the name of the one or two-digit number is added as a special case.
  5. each group of three digits can be recombined with the addition of any relevant scale number (thousand, million, billion) separated by a comma, unless the group is blank in which case it's not included at all. The exception to this rule is when the final group of three digits does not include any hundreds and there is more than one non-blank group. In this case, the final comma is replaced with and, for example one million and forty six.
  6. negative numbers are preceded by a word to indicate this negativity, for example Minus

The following function obeys all of the above rules by, firstly, splitting any given number into groups of three digits which are stored as individual elements in an array. Secondly, it then converts each of the elements from this array into its equivalent value in words, using the rules outlined above, and stores them into a new array. Finally, it takes the new array, recombines all of the elements, and applies any additional formatting to produce the returned string:


function NumberToWords(num:Number, display:Number, minus:String):String {
if (minus == null) {
minus = "Minus";
}
var smallNumbers:Array = new Array("Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen");
var tenNumbers:Array = new Array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety");
var scaleNumbers:Array = new Array("", "Thousand", "Million", "Billion");
if (num == 0) {
return smallNumbers[0];
}
var digitGroups:Array = new Array();
var positive:Number = Math.abs(num);
for (var i:Number = 0; i < 4; i++) {
digitGroups[i] = Math.floor(positive % 1000);
positive /= 1000;
}
groupText = new Array();
for (var i:Number = 0; i < 4; i++) {
groupText[i] = digitGroupToWords(digitGroups[i]);
}
function digitGroupToWords(threeDigits) {
groupText[i] = "";
var hundreds:Number = Math.floor(threeDigits / 100);
var tensUnits:Number = Math.floor(threeDigits % 100);
if (hundreds != 0) {
groupText[i] += smallNumbers[hundreds] + " Hundred";
if (tensUnits != 0) {
groupText[i] += " and ";
}
}
var tens:Number = Math.floor(tensUnits / 10);
var units:Number = Math.floor(tensUnits % 10);
if (tens >= 2) {
groupText[i] += tenNumbers[tens];
if (units != 0) {
groupText[i] += " " + smallNumbers[units];
}
} else if (tensUnits != 0) {
groupText[i] += smallNumbers[tensUnits];
}
return groupText[i];
}
var combined:String = groupText[0];
var appendAnd:Boolean;
appendAnd = (digitGroups[0] > 0) && (digitGroups[0] < 100);
for (var i:Number = 1; i < 4; i++) {
if (digitGroups[i] != 0) {
var prefix:String = groupText[i] + " " + scaleNumbers[i];
if (combined.length != 0) {
prefix += appendAnd ? " and " : ", ";
}
appendAnd = false;
combined = prefix + combined;
}
}
if (num < 0) {
combined = minus + " " + combined;
}
switch (display) {
case 0 :
// Upper case
combined = combined.toUpperCase();
break;
case 1 :
// Sentence case
combined = combined.substr(0, 1) + combined.substring(1).toLowerCase();
break;
case 2 :
// Lower case
combined = combined.toLowerCase();
break;
default :
// Capitalised
break;
}
return combined;
}

The function accepts three arguments:

  1. the number to be converted into words, e.g. 474635
  2. a value representing the case of the returned string of words:
    0 = upper case, e.g. ONE HUNDRED AND EIGHT
    1 = sentence case, e.g. One hundred and eight
    2 = lower case, e.g. one hundred and eight
    3 = capitalised, e.g. One Hundred and Eight
    The default is 3
  3. the prefix to signify a negative number, e.g. Negative
    The default value is Minus
Examples of the function's usage are shown here:


// default use
trace(NumberToWords(123456789));
// lower case
trace(NumberToWords(123456789, 1));
// upper case with an alternative negative prefix
trace(NumberToWords(-123456789, 0, "negative"));

No comments: