Vixiom Axioms

August 6, 2006

Format Price with ActionScript

Filed under: ActionScript Alastair @ 5:07 pm

ActionScript 2.0 biggest weakness is lack of support for regular expressions. This makes relatively simple things like formatting price (the American way) a bit of a chore. Here’s a solution for those not quite ready to make all their users switch to Flash Player 9.

function formatPrice(orgPrice)
{
orgPriceSpilt = orgPrice.toString();
orgPriceSpilt = orgPrice.split(“.”);
numString = orgPriceSpilt[0]
newString = “”;
index = 1;
trip = 0;
while(numString.length >= index) {
if (trip!=3)
{
//haven’t reached 3 chars yet newString = numString.charAt(numString.length - index) + newString;  //add char     index++;
trip++;
}
else { //reached 3 chars, add comma newString = “,” + newString;
trip=0;
}
}
if (orgPriceSpilt[1].length < 2)
{
decimal = orgPriceSpilt[1] + “0″ } else { decimal = orgPriceSpilt[1]
}
return (“$”+newString+“.”+decimal);
}
Digg! submit Format Price with ActionScript to stumbleupon.com submit Format Price with ActionScript to del.icio.us submit Format Price with ActionScript to reddit.com Like this post? subscribe to the feed.

1 Comment »

  1. your second line may typo

    // format price
    function formatPrice(orgPrice) {
    orgPriceSpilt = orgPrice.toString();
    orgPriceSpilt = orgPriceSpilt.split(”.”);
    numString = orgPriceSpilt[0];
    newString = “”;
    index = 1;
    trip = 0;
    while (numString.length >= index) {
    if (trip != 3) {
    //haven’t reached 3 chars yet
    newString = numString.charAt(numString.length - index) + newString;
    //add char
    index++;
    trip++;
    }
    else {
    //reached 3 chars, add comma
    newString = “,” + newString;
    trip = 0;
    }
    }
    return (newString);
    }

    Comment by yauidea — June 5, 2008 @ 8:03 pm

RSS feed for comments on this post. TrackBack URL

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image

Powered by WordPress