(Locked) rounding up numbers to two decimal places?

hi everyone,

     I am having a trouble in rounding up numbers to two decimal places.

     I have written a piece of code but it is not working properly.

     do anyone know , how to do it.

    please help me.

thanks in advance.

try this for as3


try this for as3


Same for AS2 :slight_smile: for 3 decimals its Math.round(number*1000)/1000, and so on

Hi Rahul!

Code Number of Decimal Places Example Results
value= Math.round(20/7); none 3
value= int((20/7)*10)/10; 1 2.8
value= int((20/7)*100)/100; 2 2.85
value= int((20/7)*1000)/1000; 3 2.857
value= int((20/7)*10000)/10000; 4 2.85 71

Here “20 / 7” is your number.

thanks a lot guys. :slight_smile:

I thank you all for your kind help.

rehmatullah, that’s mathematically incorect.

the ones with int()

it rounds only down

I took those lines from Adobe help page and I even tried them and they work fine. :stuck_out_tongue:

Take a look at the logic that how the calculation is being done. :slight_smile:

if it’s for cleaning up prices and outputting with a comma instead of a dot, use something like this:

function calcPrice(price:Number):String{
	//round it
	var newP:String = ((Math.round(price*100))/100).toString();
	//replace the dot for a comma
    newP = newP.split(".").join(",");
	//check if we need to serve extra zeros
    if(newP.split(",")[1].split("").length == 1){
		newP += "0";
	} else if(newP.split(",").length == 1){
		newP += ",00";
	//return the price
	return newP;

hope that helps…

edit: friggin’ smiley junk. When will FD get it, that going to pastie etc. is way too much trouble. Make a special ‘code’ tag or something.

Funny that you came up with that topic… I was trying to get my head around this as well :slight_smile:

thanks for the solution.

Who edited the post and what did you do to get that? (and off course, does it require more then 2 tags to do?)

Thanks anyways! great job!