How do you format numbers in a DataGrid? Adding commas to numbers
Right now the numbers are displayed like this:
10534.41
I would like to change it to this
10,534.41
PLEASE HELP
How do you format numbers in a DataGrid? Adding commas to numbers
Right now the numbers are displayed like this:
10534.41
I would like to change it to this
10,534.41
PLEASE HELP
This is a script from LordAlex Leon, found on Actionscript.org
I just edited a couple of lines to best suit your purposes.
formatTo = new Object();
formatTo.currency = function(amount) {
myString = amount.toString();
myDot = myString.indexOf(".");
function isRound() {
myvalue = myString;
cents = "00";
}
function isFraction() {
myvalue = myString.substr(0, myDot);
cents = myString.substr(myDot+1, myString.length);
}
function roundDecimals() {
cents = Math.round(cents.substr(0, 2)+"."+cents.substr(2, cents.length));
}
function addazero() {
if (cents.length == 1) {
cents = cents+"0";
}
}
function insertFormatting() {
if (myvalue.length>0) {
myLength = myvalue.length;
divide = myLength/3;
if ((myLength%3) == 0) {
divide = (myLength/3)-1;
}
for (var i = 1; i<=divide; i++) {
myvalue = myvalue.substr(0, (myLength-(3*i)-(i-1)))+","+myvalue.substr((myLength-(3*i)-(i-1)), (3*i)+(i-1));
myLength = myvalue.length;
}
dollars = myvalue;
}
}
myDot<0 ? isRound() : isFraction();
cents.length>2 ? roundDecimals() : addazero();
insertFormatting();
return dollars+"."+cents;
};
test = 10534.41;
trace(formatTo.currency(test));//outputs 10,534.41
Iam an idiot. I just can’t figure it out…how to add it to my fla. Can you show me? ??? PLEASE
:panda: :panda: :panda: :panda:
Here is the tutorial I originally used… (it’s the second one "Web service tutorial: Stock quotes (Flash Professional Only) "
http://livedocs.macromedia.com/flash/mx2004/main/wwhelp/wwhimpl/js/html/wwhelp.htm?href=part5_da.htm
Thank you
Hmm i dont know what a datagrid is.
hmmm, haven’t posted for a good while. can’t resist those string manipulations…
this is regular old mx code… still not up to speed on the lastest deal.
String.prototype.insert = function(str,i){
return this.substr(0,i) + str + this.substr(i);
};
Number.prototype.insertCommas = function(){
var s = this.toString().split('.');
var i = s[0].length
var c = 0;
while(i--){
if(!((++c)%3)) s[0] = s[0].insert(',',i);
};
return s.join('.');
};
test = 10534.41;
trace(test.insertCommas());
The Return of the King!! :party:
This is a joyous day indeed. Sbeener returns!
sbeener,
Where do I put your excellent coding so that the DataGrid will have columns?
It’s got nothing to do with the datagrid, it’s a prototype, you paste that in frame one (the whole proto), then anytime you want to format, instead of inserting raw data, you insert data.insertCommas();
amydor,
i don’t really work with components so their internals are a little foreign to me. there appears to be a step in the “get data” process where you can apply some formatting to the data. i think that’s most likely where you can apply these prototypes. but define them right of the bat, like eyez said.
there’s some info showing the steps on macromedia’s site here.
looks like all the data grabbed by these components is actually a String, not a Number. so you’ll want to make both these prototypes String prototypes. also you can do away with the toString() call in insertCommas().
good luck!
:: Copyright KIRUPA 2024 //--