# How do I format numbers?

**URL:** <https://forum.kirupa.com/t/how-do-i-format-numbers/64183>\
**Category:** Uncategorized\
**Created:** [September 14, 2004, 12:06am UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183 "2004-09-14T00:06:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![OM2](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@OM2](https://forum.kirupa.com/u/OM2)\
**Post date:** [September 14, 2004, 12:06am UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/1 "2004-09-14T00:06:52Z")

</div>

Can someone tell me how I can format numbers in Flash so that it will show two decimal places regardless of the number itself?

For example, if I have 4.5, then I would want this to be displayed as 4.50.

Thanks.

OM

---

<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:** [September 14, 2004, 3:33am UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/2 "2004-09-14T03:33:49Z")

</div>

hi,

here’s a function I wrote:

```auto

// function f, returns a string
// n is the number you want to convert
// d is the number of decimal places
function f(n, d)
{
	var x = Math.round(n * Math.pow(10, d));
	var y = x / Math.pow(10, d);
	// convert number to string
	y = new String(y);
	// length of the string
	var len = y.length;
	// position of the .
	var pos = y.indexOf(".");
	// does the . exist?
	if (pos != -1) {
		// add zeros
		for (var i = (len - pos - 1); i < d; i++) {
			y += "0";
		}
		// if it doesn't
	}
	else {
		if (d > 0) {
			// add .
			y += ".";
			// add zeros
			for (var i = 1; i <= d; i++) {
				y += "0";
			}
		}
	}
	return y;
}

// usage
myNumber = f(3.5, 2);
trace(myNumber); // 3.50

```

---

<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:** [September 14, 2004, 11:04am UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/3 "2004-09-14T11:04:49Z")

</div>

THANK YOU!  
But… surely there’s another way of doing this!!?  
I was hoping not to have to do what you’ve done.  
I haven’t found any answers anywhere else.  
So… your code is **MUCH** appreciated.  
It means that I won’t have to write it myself now.

---

<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:** [September 14, 2004, 5:28pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/4 "2004-09-14T17:28:41Z")

</div>

Something like this ?

```auto

Number.prototype.setDecimals = function(decs){
	return new String(Math.floor(this)+"."+(this-Math.floor(this))*Math.pow(10,decs));
}
nr = 4.5;
trace(nr.setDecimals(2))

```

---

<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:** [September 14, 2004, 5:40pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/5 "2004-09-14T17:40:37Z")

</div>

Do you have a hard time getting through doorways with that big brain of yours? =)

---

<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:** [September 14, 2004, 5:42pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/6 "2004-09-14T17:42:26Z")

</div>

Not yet, but it tends to get heavy to carry  
(especially at school during boring lessons) 😛

---

<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:** [September 14, 2004, 6:14pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/7 "2004-09-14T18:14:05Z")

</div>

thanks for the reply.  
but it doesn’t quite function as I want it.  
try the following:

```auto

Number.prototype.setDecimals = function(decs)
{
return new String(Math.floor(this)+"."+(this-Math.floor(this))*Math.pow(10,decs));
}
nr = 4.05;
trace(nr.setDecimals(2));
 

```

a number like 4.05, should of course be displayed as it is.

anymore ideas?

---

<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:** [September 14, 2004, 6:19pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/8 "2004-09-14T18:19:45Z")

</div>

How is 4.05 displayed? I don’t have Flash on this computer, so I can’t try it for myself.

---

<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:** [September 14, 2004, 6:20pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/9 "2004-09-14T18:20:00Z")

</div>

hi Voetsjoeba,

Your script doesn’t work for numbers like  
4.0  
0  
and  
45.3463457345

[edit]  
I guess I’ve been too slow complaining  
[/edit]

---

<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:** [September 14, 2004, 6:23pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/10 "2004-09-14T18:23:35Z")

</div>

Nevermind, 4.05 -\> 4.5 since the 0 gets lost after multiplying by 100.

---

<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:** [September 14, 2004, 6:23pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/11 "2004-09-14T18:23:41Z")

</div>

4.05 results in 4.4.99999999999998.

---

<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:** [September 14, 2004, 6:27pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/12 "2004-09-14T18:27:33Z")

</div>

Couldn’t you just do:

roundedvalue = int(originalvalue\*100)/100;

---

<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:** [September 14, 2004, 6:28pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/13 "2004-09-14T18:28:27Z")

</div>

> [@OM2](#):
>
> thanks for the reply.  
> but it doesn’t quite function as I want it.  
> try the following:
> 
> ```auto
> 
> Number.prototype.setDecimals = function(decs)
> {
> return new String(Math.floor(this)+"."+(this-Math.floor(this))*Math.pow(10,decs));
> }
> nr = 4.05;
> trace(nr.setDecimals(2));
>   
>  
> 
> ```
> 
> a number like 4.05, should of course be displayed as it is.
> 
> anymore ideas?

Yes, that’s annoying, but it’s nothing I can do about. Look:

```auto

 Number.prototype.setDecimals = function(decs) {
     var base = Math.floor(this)
     trace("base: "+base);
     trace("diff: this("+this+") - base("+base+") = "+(this-Math.floor(this)))
     return new String(Math.floor(this)+"."+(this-Math.floor(this))*Math.pow(10, decs));
 };
 nr = 4.05;
 trace(nr.setDecimals(2));
 

```

Outputs 0.4999999998 as difference, and not 0.5. Not much something I can do about. Guess I’ll have to do it manually …

---

<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:** [September 14, 2004, 6:29pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/14 "2004-09-14T18:29:05Z")

</div>

take a look at the prototypes available at [layer51.com](http://proto.layer51.com/l.aspx?p=7).

---

<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:** [September 14, 2004, 6:30pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/15 "2004-09-14T18:30:39Z")

</div>

i kinda figured out a cheating way of achieving what i need to do.  
all my numbers will be monetary ones.  
so i’ll never have more than two decimal places.  
so… all i have to do is write a function that checks for the number of digits after the decimal point.  
if there is only one, then add a 0.  
i think that should do the trick for me?

but… i still can’t believe that flash doesn’t have any inherent functions to format numbers as in excel for example.

---

<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:** [September 14, 2004, 6:30pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/16 "2004-09-14T18:30:48Z")

</div>

> [@hificopymaster](#):
>
> Couldn’t you just do:
> 
> roundedvalue = int(originalvalue\*100)/100;

No, because if you add 5 decimals to 4.05, it’ll make it 4.0500000, but since it’s a number, it’ll return it as 4.05 again. That method is good for moving the comma, not for adding decimals.

---

<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:** [September 14, 2004, 6:35pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/17 "2004-09-14T18:35:33Z")

</div>

> [@Voetsjoeba](#):
>
> No, because if you add 5 decimals to 4.05, it’ll make it 4.0500000, but since it’s a number, it’ll return it as 4.05 again. That method is good for moving the comma, not for adding decimals.

Where did I add 5 decimals? I multiplied by 100, turned it into an int, and then divided it by 100 again.

Okay, hang on. If you run it with 4.05, it goes like this:  
roundedvalue = int(4.05\*100)/100;  
roundedvalue = int(405)/100;  
roundedvalue = 405/100;  
roundedvalue = 4.05;

And if you run it with something like 4.5028204, you get:  
roundedvalue = int(4.5028204\*100)/100;  
roundedvalue = int(450.28204)/100;  
roundedvalue = 450/100;  
roundedvalue = 4.5;

… Hmmm… you’re right. It doesn’t work. Well as far as I can see, it works unless there’s a 0 in the hundredths position.

---

<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:** [September 14, 2004, 7:04pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/18 "2004-09-14T19:04:57Z")

</div>

Ok dudes, try this one :beam:

```auto

Number.prototype.toDecimals = function(decs){
	if(decs >= 0 && !isNaN(decs)){
		var _this = this.toString();
		var ordecs = _this.substr(_this.indexOf(".")+1,_this.length).length;
		if(decs > ordecs){
			for(var q=0;q<(decs-ordecs);q++) _this += "0";
		} else {
			_this = _this.substr(0,(decs == 0) ? -ordecs-1 : decs-ordecs);
		}
		return _this;
	} else {
		trace("Error: invalid decimals count requested.");
	}
}
nr = 4.0512354
trace(nr.toDecimals(2))

```

---

<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:** [September 14, 2004, 7:21pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/19 "2004-09-14T19:21:30Z")

</div>

thank you!  
ur a genious. 🙂

---

<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:** [September 14, 2004, 7:26pm UTC](https://forum.kirupa.com/t/how-do-i-format-numbers/64183/20 "2004-09-14T19:26:15Z")

</div>

:love:
