mperrah
09-11-2001, 09:22 AM
I found this code in the ActionScripting in Flash book by Kerman. I keep getting $0.00 as the result which is the right format but the wrong result. The code didn't work at all at first but I found (I think) some typos- they had "substr" in one spot and subStr in another and substring in a third. Anyway I'll let You the Guru do your voodoo. Here's 3 functions plus the button calling them.
//Adds a "0" in front of an amount if cents only
function pad (num, places) {
theString = String(num)
for (i=0; i<places; i++) {
if (theString.length<places) {
theString = "0"+theString;
}
}
return theString;
}
//Adds "00" or "0" to the right until 2 places are present
function trail (num, places) {
var theString = String(num)
for (i=0; i<places; i++) {
if (theString.length>=places) {
break;
} else {
theString = theString+"0";
}
}
return theString;
}
//Accounts for rounding errors and formats to $0.00 format
function currency (num) {
var theString = String(rnd(num, 2));
var decimalLoc = theString.indexOf(".");
var dollars;
var cents;
if (decimalLoc == -1) {
dollars = theString;
cents = "00";
} else {
dollars = theString.substring(0, decimalLoc);
cents = theString.substring(decimalLoc+1);
}
cents = trail(cents, 2);
dollars = pad(dollars, 1);
var actualDollars = "";
var thousands = Math.floor((dollars.length)/3);
var extra = (dollars.length)%3;
if (extra>0) {
actualDollars = actualDollars+dollars.substring(0, extra);
}
if (thousands>0) {
if (extra>0) {
actualDollars = actualDollars+",";
}
for (i=1; i<=thousands; i++) {
theseThree = dollars.substring(extra+((i-1)*3), 3);
actualDollars = actualDollars+theseThree;
if (i<thousands) {
actualDollars = actualDollars+",";
}
}
}
return "$"+actualDollars+"."+cents;
}
//Calls functions using input (original, rate) and
//dynamic (result) text boxes
on (release) {
decimals = 1;
decimals += (original.length-1) - original.lastIndexOf(".");
decimals += (rate.length-1) - rate.lastIndexOf(".");
fudge = 1/Math.pow(10, decimals);
result = currency((original*rate) + fudge);
}
//Adds a "0" in front of an amount if cents only
function pad (num, places) {
theString = String(num)
for (i=0; i<places; i++) {
if (theString.length<places) {
theString = "0"+theString;
}
}
return theString;
}
//Adds "00" or "0" to the right until 2 places are present
function trail (num, places) {
var theString = String(num)
for (i=0; i<places; i++) {
if (theString.length>=places) {
break;
} else {
theString = theString+"0";
}
}
return theString;
}
//Accounts for rounding errors and formats to $0.00 format
function currency (num) {
var theString = String(rnd(num, 2));
var decimalLoc = theString.indexOf(".");
var dollars;
var cents;
if (decimalLoc == -1) {
dollars = theString;
cents = "00";
} else {
dollars = theString.substring(0, decimalLoc);
cents = theString.substring(decimalLoc+1);
}
cents = trail(cents, 2);
dollars = pad(dollars, 1);
var actualDollars = "";
var thousands = Math.floor((dollars.length)/3);
var extra = (dollars.length)%3;
if (extra>0) {
actualDollars = actualDollars+dollars.substring(0, extra);
}
if (thousands>0) {
if (extra>0) {
actualDollars = actualDollars+",";
}
for (i=1; i<=thousands; i++) {
theseThree = dollars.substring(extra+((i-1)*3), 3);
actualDollars = actualDollars+theseThree;
if (i<thousands) {
actualDollars = actualDollars+",";
}
}
}
return "$"+actualDollars+"."+cents;
}
//Calls functions using input (original, rate) and
//dynamic (result) text boxes
on (release) {
decimals = 1;
decimals += (original.length-1) - original.lastIndexOf(".");
decimals += (rate.length-1) - rate.lastIndexOf(".");
fudge = 1/Math.pow(10, decimals);
result = currency((original*rate) + fudge);
}