all groups > flash actionscript > february 2006 >
You're in the

flash actionscript

group:

Dynamic Text - Formatting Numbers to Look Right


Dynamic Text - Formatting Numbers to Look Right nivek311
2/16/2006 6:36:55 PM
flash actionscript:
I'm loading a number value from an external .txt file. The data feed however
doesn't use english nomenclature, so the value I'm getting is like this for
example: 19873328347201. I want to somehow make flash automatically optimize
that number it pulls in to look like this: 19,873,328,347,201 so that its more
readable. Is this possible?

I searched the forum and haven't found anything about this. Thank you.
Re: Dynamic Text - Formatting Numbers to Look Right blemmo
2/17/2006 1:35:59 PM
I don't know of any built-in formatting, but you can use this code to add a ","
every 3 numbers. After that, you'll have a formatted string in 's' (not a
number!).

cheers,
blemmo



s = String(num);
i = s.lastIndexOf(".");
if (i == -1) i = s.length-4;
else i-=4;
for(i;i>0;i-=3){
s = s.substr(0,i+1) + "," + s.substr(i+1);
}
Re: Dynamic Text - Formatting Numbers to Look Right nivek311
2/20/2006 4:49:09 PM
Re: Dynamic Text - Formatting Numbers to Look Right blemmo
2/20/2006 6:42:59 PM
The code I posted is only for formatting of a given number ('num'), 's' is a
temporary String inside that function. In your case, the input should already
be a String.
You'll have to load the input first and then format it. I changed your code,
although I'm not sure what you want to accomplish. What is the interval for? If
you don't kill it, it will always return 'undefined' once you went through all
rows.

cheers,
blemmo



//Settings
var interval = 1;
var output_list;
var row = 0;
var file = 'output.txt';

//Functions
nextOutput = function () {
text_field.text = format(output_list[row]);
row++; //not sure if you wanted this
};

format = function(s:String) {
i = s.lastIndexOf(".");
if (i == -1) {
i = s.length-4;
} else {
i -= 4;
}
for (i; i>0; i -= 3) {
s = s.substr(0, i+1)+","+s.substr(i+1);
}
return s;
}

//Get input
my_lv = new LoadVars();
my_lv.onData = function(raw) {
output_list = raw.split('\r\n');
container.alphagoal = 0;
setInterval(nextOutput, interval*1000);
};
my_lv.load(file);
Re: Dynamic Text - Formatting Numbers to Look Right nivek311
2/20/2006 7:02:50 PM
Blemmo - thanks for your help on this. I think this is goign to get me what I want. I will tweak to accomodate the specifics of what I'm doing.

Re: Dynamic Text - Formatting Numbers to Look Right nivek311
2/20/2006 9:36:03 PM
Blemmo - this is what I ended up doing to make this function appropriately
depending on any number of digits.

//Settings
var interval = 1;
var output_list;
var row = 0;
var file = 'output.txt';
//Functions
nextOutput = function () {
text_field.text = format(output_list[row]);
};
format = function (s:String) {
i = s.lastIndexOf(".");
if (i == -1) {
i = s.length-4;
} else {
i -= 4;
}
r = (s.length%3);
if (r>0) {
s = s.substr(0, r)+","+s.substr(r);
i += 1;
}
for (i; i>r; i -= 3) {
s = s.substr(0, i+1)+","+s.substr(i+1);
}
return s;
};
//Get input
my_lv = new LoadVars();
my_lv.onData = function(raw) {
output_list = raw.split('\r\n');
nextOutput();
};
my_lv.load(file);
AddThis Social Bookmark Button