Rounding to 2 decimal places with Math.round function.
A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Rounding to 2 decimal places with Math.round function.

  1. #1
    Member
    Join Date
    May 2001
    Posts
    60

    Exclamation

    Hey Everyone

    How do you round a number to 2 decimal places using the Math.round function? Thanks in advance.

    -turner2000

  2. #2
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,361
    Math.round(number*100)/100

  3. #3
    Member
    Join Date
    Apr 2007
    Posts
    56
    this works OK but if the number is say 12.40 the result will be 12.4. I have tried using "int" instead of round and it strangely takes 12.40 and displays 12.39 ?????

  4. #4
    Moderator whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,697
    cant you check for the decimal?..and if NOT present..add it to the end?

    something like this: (or alternative)

    actionscript Code:
    var targetNumber:Number = 27.40;
    var tempNum = Math.round(targetNumber*100)/100;

    var stringNum:String = String(tempNum);
    if(stringNum.charAt(stringNum.length-2) != "0"){
        stringNum += "0";
        trace("NEW NUMBER: "+stringNum)
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width


HTML5 Development Center

Poll by Flashkit.com