PDA

View Full Version : [AS2] Flash Game Display force to 2 decimal place


bo0
02-25-2009, 10:10 AM
Hi all,

I bought a flash game and want to change the display to 2 decimal place but i do not know how.
Example, 0.00 instead of 0, 1.12 instead of 1.1234

The Var field for the Dynamic Text is cash

For the actionscript, i will paste all the codes related to the var cash

var cash:Number = 0;

if(_root.currentBlock._currentframe<20 && _root.currentBlock._visible==true){
_root.currentBlock.gotoAndPlay(20);
if(_root.currentBlock._name=="w"){
if(level==1){
cash+=.03;
}
if(level==2){
cash+=.05;
}
if(level==3){
cash+=.10;
}
}else{
gameOver();
}

onEnterFrame=function(){
mcProgress.gotoAndStop(Math.ceil(cash*20));
if(paused){
return;


any help by the pros here wld be appreciated. Thanks! :)

bluemagica
02-26-2009, 05:32 AM
Math.round(cash*100)/100

atomic
02-26-2009, 06:49 AM
//format a number into specified number of decimal places
Math.formatDecimals = function (num, digits) {
//if no decimal places needed, we're done
if (digits <= 0) {
return Math.round(num);
}
//round the number to specified decimal places
//e.g. 12.3456 to 3 digits (12.346) -> mult. by 1000, round, div. by 1000
var tenToPower = Math.pow(10, digits);
var cropped = String(Math.round(num * tenToPower) / tenToPower);

//add decimal point if missing
if (cropped.indexOf(".") == -1) {
cropped += ".0"; //e.g. 5 -> 5.0 (at least one zero is needed)
}

//finally, force correct number of zeroes; add some if necessary
var halves = cropped.split("."); //grab numbers to the right of the decimal
//compare digits in right half of string to digits wanted
var zerosNeeded = digits - halves[1].length; //number of zeros to add
for (var i=1; i <= zerosNeeded; i++) {
cropped += "0";
}
return(cropped);
} //Robert Penner May 2001 - source@robertpenner.com


// Usage...

You want to force 2 decimal places to the number 1.1

my_num2 = Math.formatDecimals(1.1,2);
trace (my_num2); // Outputs 1.10