(Locked) rounding up numbers to two decimal places?


#1

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.


#2

try this for as3

Math.round(number*100)/100


#3
try this for as3

Math.round(number*100)/100

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


#4

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.


#5

thanks a lot guys. :slight_smile:

I thank you all for your kind help.


#6

rehmatullah, that’s mathematically incorect.

the ones with int()
:stuck_out_tongue:

it rounds only down


#7

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:


#8

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.


#9

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.


#10

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!


#11