PDA

View Full Version : Returning a dollar value

Whelan_Web
04-30-2003, 01:39 AM
Hello is their anyone out their who can tell me how to format a value in a dynamic text box so that it ruturns a dollar value

example when it does a calculation it returns in the text box something like

1000.236 but what I need is \$1000.23 or 1000.23

thanks alot

David

jefmyers
04-30-2003, 04:10 AM
not sure if this is what your talking about

x = 1000.236
y = Math.round(x * 100) / 100
trace(y)

don't konw if this helps or not. it rounds up to the cent. you might format it diff, i don't know what you want to do with it?
-jef

CyanBlue
04-30-2003, 04:19 AM
Howdy... ;)

You could search the forum to find more information faster... ;)

I did search the forum with 'currency', and I think this thread (http://www.actionscript.org/forums/showthread.php3?s=&threadid=25131) could be useful for you... ;)

Keyword: currency cbcurrency

realdude
02-09-2005, 02:17 PM
Greetings,

I have searched and read everything I can find, both in this forum and just about everywhere else, and can't seem to find a solution. Is there a way to format a text input field so that when the field is exited the numbers, at the very least, is formatted with commas? Please be gentle, struggling student here.

Thanks

tg
02-09-2005, 06:44 PM
http://proto.layer51.com/d.aspx?f=164

realdude
02-09-2005, 07:49 PM
Sorry to be so dense, but I'm not quite sure how to apply this. Let me take a stab at it and please correct me if I'm wrong.

1. create a frame, actions and past the code there
2. create a text field (input or dynamic)
3. Name the field, say input1
4. What next? How do I call the function?

Boy, do I feel dumb!

Thanks

speedlemon
02-12-2005, 07:34 PM
onEnterFrame = function () {
outputtext = "\$"+Math.round(inputtext*100)/100;
};

problem solved.

btw to call a function-

my_function()

tg
02-13-2005, 07:46 PM
try it this way.... then you don't have the loop running in the background.

/************************ prototypes ********************************/
// author: Justlar [+], Submitted: 06.17.01 3p
//http://proto.layer51.com/d.aspx?f=164
Number.prototype.Currency = function(uom) { // uom = Unit of Measure. Default set to US Dollars (\$).
var temp = this.toString();
var dot = temp.indexOf(".", 0);
if(uom==null) { uom="\$" }
if (dot<=0) {
var thedollar = temp;
var cents = "00";
} else {
var thedollar = temp.substr(0,dot);
var cents = temp.substr(dot+1,temp.length);
}
if (thedollar.length>3) {
var d_Len = thedollar.length;
var d_brk = d_Len/3;
if ((d_Len % 3)==0) {
var d_brk = (d_Len/3)-1;
}
for (var i=1; i<=d_brk; i++) {
var thedollar = thedollar.substr(0,(d_Len-(3*i)-(i-1))) + "," + thedollar.substr((d_Len-(3*i)-(i-1)),(3*i)+(i-1));
d_Len = thedollar.length;
}
}
dollars = thedollar;
if (cents.length>2) {
cents = Math.round(cents.substr(0,2) + "." + cents.substr(2,cents.length));
} else if (cents.length==1){
cents = cents + "0";
}
return uom + dollars + "." + cents;
}

// author: moock
//http://www.oreillynet.com/pub/a/javascript/2003/05/20/colinmoock.html
TextField.prototype.onKeyDown = function () {
if (Key.getCode() == Key.ENTER) {
changeTextToDollars();
//trace('enter key hit');
}
};
changeTextToDollars=function(){
var cur=Number(dollars_txt.text);
//trace(cur);
dollars_txt.text=cur.Currency();
};
Key.addListener(dollars_txt);

note. there was a bug in currency prototype, but it is fixed in this code. (it wasnt showing dollar values less than 100

i also added a textfield method written by colin moock. this allows the textfield to capture when the enter key was hit. so that the user can enter their dollar amount and hit enter, and the currency will format.

the keylistener allows the specified textfield to 'listen' for the any key to be hit.

you can also add a 'submit button' and have that call the changetexttodollars function also.

to get this code to work for you do the following:
copy and paste the above code into a new fla.
put an input box on the stage.
give the input box an instance name of 'dollars_txt'

you can also change it a bit so when the dollars_txt looses focus, the input is formatted. you may also want to put some error handling in there so that can catch when someone puts something other than a number in the input box.
it should work fine. it does for me.