**HELP** Flash Actionscripting
- Started
- Last post
- 14 Responses
- Dcsign
i have a dynamic ouput field generating a number from a calculation.
I need this output figure to be formated with commas every 3 figures and rounded up or down so there is no bullet point.
any help... please....
thanks...much appreciated..
Dan
- Dcsign0
pleasssee help.......
- Dcsign0
someone has to be an actionscript guru in here
...please...... please.... help....
- stewart0
maybe your question is not that clear ?
- Dcsign0
just for clarification...
I need to format a number which is created by flash to display with commas every three digits and round up or down so as not to display the decimal point.
Any help MUCH APPRECIATED :)
- goygoygoy0
"...and round up or down so as not to display the decimal point. "
you has to use Math.floor(yourNumber); (quit the decimal and round to the smallest nb) or Math.ceil(yourNumber); (quit the decimal and round to the greatest nb)
the rest of the ? is a little bizare bizare. hope it helps
- Dcsign0
greg, goygoygoy THANKS v.much.... that was very helpful. What i meant was i need that number which i have now rounded up or down needs to be displayed in 3 digit intervals (eg: 123, 456, 789)
Any idea...?
Thanks again though
- JamesEngage0
you want to take the rounded number turn it into a string, an use substr, or substring, to construct a new string...can't remember which one it is... but it's not hard...
somelike origString.substr(0,3) will take the first three digits...
at (3,3) will take the next three.... so you could probably do something like
outputString = origString.substr(0,3) + "," + origString.substr(3,3)+ "," + origString.substr(6,3)
you'll need to play around with it... and amke it cool by knowing that a four digit number has it like 1,000 .... so infact you want to work from the end of the string in threes...
quite a good puzzle, if I had time I'd do it for you... but I haven't, but good luck :)
- magicspoon0
Hey-
I actually just did this for a project.
You basically want to do what everyone else is saying.
What i did (not saying this is the best way to do it- just what i did):1. to get 2 decimal place output (i.e.$34.50 not 34.564565)
You multiply the number by 100, round it, then divide by a 1002. to add commas in, you make it a string, split it at the '.' and from there you loop through the myString[0] that is made(left gobbleygook of decimal), and create a local increment var that loops through the string, counts to 3, adds a comma, reset the increment var, and continue like so ad infinitum until you get to the end of your number string.
- unfittoprint0
enjoy.
//format Number function
function formatNumber(calcNumber) {
calcString = Math.round(calcNumber).toString...
while (calcString.length>0) {
calcFormat = (calcString.length>3) ? "."+calcString.substr(-3, 3)+calcFormat : calcString.substr(-3, 3)+calcFormat;
calcString = calcString.slice(0, calcString.length-3);
}
return calcFormat;
}//insert text in textfield
result.text = formatNumber(1234567.232);
- Dcsign0
shucks.... you guys are great..... what a community.....
thanks v.much... i would offer a few beers if i knew who you guys where.. lol
- JamesEngage0
you know me ... you ripped our site (JOKING! ... remember the post months back) :)
- SmilingBuddh0
A minute late, I suppose. Pretty much the same as unfit's:
Number.prototype.splitWithCommas = function() {
var xa = Math.round(this).toString().spli...
var ta = [];
for (var n = xa.length; n>=0; n--) {
ta.push(xa[n]);
if ((n%3) == 0 && n != 0) {
ta.push(",");
}}
return(ta.join(""));
};
- unfittoprint0
nice one, Jer. Pushing a prototype into a Number class may actually be more practical...
- SmilingBuddh0
I'm a bit pissed off about the hack there with the for loop.
I couldn't see why
for (n in xa) {}
wouldn't work, but apparently the reverse() method fucks with the indexing somehow.
oh well.
maybe i'll look at it again when I'm actually awake.