PDA

View Full Version : Math to 2 decimal places



Lacuna
January 3rd, 2004, 05:03 PM
Im thinkn' its around but I just cant search it...

Sooo....

What is the Math.???? for making a number always have 2 decimal places?


Im sorry if it's simple, I've had a rough day :c)

~~ Seretha :love:

e.s.x.s
January 3rd, 2004, 05:26 PM
math is a built in class in flash and it has too many methods... with the math you can use trigonometry ,logarithm and etc. or u can compute square root of a number...
so what is the Math?
it is AS's Mathematics genius:)

ahmed
January 3rd, 2004, 05:34 PM
multiply a number by a 100, round it, and divide it by a 100:
Math.roundTo = function(value, precision)
{
return Math.round(value*Math.pow(10, precision))/Math.pow(10, precision);
};

trace(Math.roundTo(Math.PI, 3));// traces 3.142


[EDIT]

oops.. why the hell did I think "rounding".. lol

norie
January 3rd, 2004, 05:35 PM
Originally posted by e.s.x.s
math is a built in class in flash and it has too many methods... with the math you can use trigonometry ,logarithm and etc. or u can compute square root of a number...
so what is the Math?
it is AS's Mathematics genius:)

she wants to know, if there is an actionscript to keep all numbers to have 2 decimal places (ie. money) 2.00, 1.00, 1.20 ....

Hrmm, you'd think flash would have a built in float property for numbers but.. i guess not.... this has been asked many times, i'll see if i can find something, if i can't, i'll try and whip something up real quick like. :)

norie
January 3rd, 2004, 05:36 PM
Originally posted by ahmed
multiply a number by a 100, round it, and divide it by a 100:
Math.roundTo = function(value, precision)
{
return Math.round(value*Math.pow(10, precision))/Math.pow(10, precision);
};

trace(Math.roundTo(Math.PI, 3));// traces 3.142


yea i was thinkin' that too
to quick for me :)

Kole
February 24th, 2004, 08:34 PM
If, however, you are turning Fractions into Percentiles . . .

Say you have shot 30 UFO's and missed 5 ...

You will require this :


a = hit;
b = hit+miss;
temp = (a/b) * 1000;
percentile = Math.round(temp) /100 +"%";

For a PerMille its :


a = hit;
b = hit+miss;
temp = (a/b) * 10000;
percentile = Math.round(temp) /100 +"‰";

This is good if you are working on a Scoring System

You can make the code smaller, but I have broke it into it's stages for ease of understanding.

/*****
* Edit *
*****/

Just realised, if you wanted to make the float a bit longer just increase the Multipliers by a multiple of 10

e.g.

To 3d.p.


a = hit;
b = hit+miss;
temp = (a/b) * 100000;
percentile = Math.round(temp) /1000 +"%";


Hope this helps ya :smirk: