Format Price with ActionScript
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);
}




Like this post? subscribe to the feed.




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