PhotoDune

rounding up numbers to two decimal places?

1541 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • India
  • Referred between 1 and 9 users
  • Sold between 100 and 1 000 dollars
rahulbist says

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.

3 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • South Africa
illustrator says

try this for as3

Math.round(number*100)/100

1954 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Netherlands
  • Referred between 10 and 49 users
  • Sold between 5 000 and 10 000 dollars
Emroni says
try this for as3 Math.round(number100)/100

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

Animated_Creativity
Animated_Creativity Recent Posts
Threads Started
479 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • India
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
Animated_Creativity says

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.

1541 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • India
  • Referred between 1 and 9 users
  • Sold between 100 and 1 000 dollars
rahulbist says

thanks a lot guys. :)

I thank you all for your kind help.

23 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • Sold between 100 and 1 000 dollars
encoder says

rehmatullah, that’s mathematically incorect.

the ones with int() :P

it rounds only down

Animated_Creativity
Animated_Creativity Recent Posts
Threads Started
479 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • India
  • Referred between 1 and 9 users
  • Sold between 10 000 and 50 000 dollars
Animated_Creativity says

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

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

279 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Netherlands
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 10 000 and 50 000 dollars
FonZ says

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.

useyourimagination
useyourimagination Recent Posts
Threads Started
21 posts
  • Bought between 10 and 49 items
  • Europe
  • Has been a member for 4-5 years
useyourimagination says

Funny that you came up with that topic.. I was trying to get my head around this as well :-)

thanks for the solution.

279 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Netherlands
  • Referred between 10 and 49 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 10 000 and 50 000 dollars
FonZ says

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!

by
by
by
by
by