# Format Numbers

**URL:** <https://forum.kirupa.com/t/format-numbers/42309>\
**Category:** flash\
**Created:** [February 7, 2004, 4:40pm UTC](https://forum.kirupa.com/t/format-numbers/42309 "2004-02-07T16:40:41Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AmyDor](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@AmyDor](https://forum.kirupa.com/u/AmyDor)\
**Post date:** [February 7, 2004, 4:40pm UTC](https://forum.kirupa.com/t/format-numbers/42309/1 "2004-02-07T16:40:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 7, 2004, 11:18pm UTC](https://forum.kirupa.com/t/format-numbers/42309/2 "2004-02-07T23:18:01Z")

</div>

This is a script from LordAlex Leon, found on [Actionscript.org](http://Actionscript.org)  
I just edited a couple of lines to best suit your purposes.

```auto
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

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 1:17am UTC](https://forum.kirupa.com/t/format-numbers/42309/3 "2004-02-08T01:17:39Z")

</div>

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](http://livedocs.macromedia.com/flash/mx2004/main/wwhelp/wwhimpl/js/html/wwhelp.htm?href=part5_da.htm)

Thank you

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 1:39am UTC](https://forum.kirupa.com/t/format-numbers/42309/4 "2004-02-08T01:39:21Z")

</div>

Hmm i dont know what a datagrid is.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 7:34pm UTC](https://forum.kirupa.com/t/format-numbers/42309/5 "2004-02-08T19:34:41Z")

</div>

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. ☹

```auto

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());

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 7:53pm UTC](https://forum.kirupa.com/t/format-numbers/42309/6 "2004-02-08T19:53:41Z")

</div>

The Return of the King!! :party:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 8:10pm UTC](https://forum.kirupa.com/t/format-numbers/42309/7 "2004-02-08T20:10:25Z")

</div>

This is a joyous day indeed. Sbeener returns!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 8:25pm UTC](https://forum.kirupa.com/t/format-numbers/42309/8 "2004-02-08T20:25:28Z")

</div>

sbeener,

Where do I put your excellent coding so that the DataGrid will have columns?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 8, 2004, 10:47pm UTC](https://forum.kirupa.com/t/format-numbers/42309/9 "2004-02-08T22:47:17Z")

</div>

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();

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 9, 2004, 6:48am UTC](https://forum.kirupa.com/t/format-numbers/42309/10 "2004-02-09T06:48:16Z")

</div>

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](http://livedocs.macromedia.com/flash/mx2004/main/wwhelp/wwhimpl/js/html/wwhelp.htm?href=part5_da.htm).

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!
