# How do I format numbers?

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

hi,

here’s a function I wrote:

``````
// 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) {
for (var i = (len - pos - 1); i < d; i++) {
y += "0";
}
// if it doesn't
}
else {
if (d > 0) {
y += ".";
for (var i = 1; i <= d; i++) {
y += "0";
}
}
}
return y;
}

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

``````

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.

Something like this ?

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

``````

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

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

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

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

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

hi Voetsjoeba,

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

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

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

4.05 results in 4.4.99999999999998.

Couldn’t you just do:

roundedvalue = int(originalvalue*100)/100;

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

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

take a look at the prototypes available at layer51.com.

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.

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.

Ok dudes, try this one :beam:

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

``````

thank you!
ur a genious.

:love: