Wednesday, August 6, 2008

Formatting currency

Following on from the previous entry about formatting numbers, it's a simple task to convert the same function into one that accepts currency symbols:


function formatCurrency(num:Number, comma:Boolean, currency:String):String {
// return a zero value if num is not valid
if (isNaN(num)) {
return "0.00";
}
// return a blank value if currency is not valid
if (currency == undefined) {
currency = "";
}
// round num to the nearest 100th
num = Math.round(num * 100) / 100;
// convert num to a string
var num_str:String = String(num);
// seperate any decimals from the whole numbers
var num_array = num_str.split(".");
// if there are no decimals add them using "00"
if (num_array[1] == undefined) {
num_array[1] = "00";
}
// if the decimals are too short, add an extra "0"
if (num_array[1].length == 1) {
num_array[1] += "0";
}
// separate whole numbers with commas
// if required (comma = true)
if (comma) {
var whole_array:Array = new Array();
var start:Number;
var end:Number = num_array[0].length;
while (end > 0) {
start = Math.max(end - 3, 0);
whole_array.unshift(num_array[0].slice(start, end));
end = start;
}
num_array[0] = whole_array.join(",");
}
// construct a return string joining
// the whole numbers with the decimals
// and prefixing a currency symbol
return (currency + num_array.join("."));
}
trace(formatCurrency(1234.5, true, "£"));
// outputs £1,234.50
trace(formatCurrency(1234.5, true, "\u00a5"));
// outputs ¥1,234.50

Formatting numbers

This one comes up a lot - how to format a number with commas and decimal points? Just pass your number into this function and it will return a formatted string. Including commas is optional.


function formatNumbers(num:Number, comma:Boolean):String {
// return a zero value if num is not valid
if (isNaN(num)) {
return "0.00";
}
// round num to the nearest 100th
num = Math.round(num * 100) / 100;
// convert num to a string
var num_str:String = String(num);
// seperate any decimals from the whole numbers
var num_array = num_str.split(".");
// if there are no decimals add them using "00"
if (num_array[1] == undefined) {
num_array[1] = "00";
}
// if the decimals are too short, add an extra "0"
if (num_array[1].length == 1) {
num_array[1] += "0";
}
// separate whole numbers with commas
// if required (comma = true)
if (comma) {
var whole_array:Array = new Array();
var start:Number;
var end:Number = num_array[0].length;
while (end > 0) {
start = Math.max(end - 3, 0);
whole_array.unshift(num_array[0].slice(start, end));
end = start;
}
num_array[0] = whole_array.join(",");
}
// construct a return string joining
// the whole numbers with the decimals
return (num_array.join("."));
}
trace(formatNumbers(1234));
// outputs 1234.00
trace(formatNumbers(1234.56));
// outputs 1234.56
trace(formatNumbers(1234.5, true));
// outputs 1,234.50
trace(formatNumbers(-1234.56789, true));
// outputs -1,234.57

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"));

Saturday, August 2, 2008

Delay and jump to new frame

A frequent problem is how to delay the timeline, for a given period of time, and then branch to another frame once the delay is up.

A bespoke function and the setTimeout function can be combined to do this with ease:


stop();
var delay:Number = 1000;
var frameNumber:Number = 10;
function playFrame(frameNumber:Number)
{
gotoAndStop(frameNumber);
}
setTimeout(playFrame, delay, frameNumber);

delay is the length of time before the setTimeout calls the playFrame function. This is expressed in milliseconds.
frameNumber is the target frame to jump to once the delay is over.

The same principle works equally as well with frame labels:


stop();
var delay:Number = 2000;
var frameLabel:String = "menu";
function playFrame(frameLabel:String)
{
gotoAndStop(frameLabel);
}
setTimeout(playFrame, delay, frameLabel);