A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Rounding off Schmounding Off

  1. #1

    Smile

    I am building a pre loader with heaps of stats and stuff, and need a simple query answered.
    I need to know how to round off a number (or variable) to a certain amount of decimal places. I know how to round it off to a whole number.
    Code:
        kbs = (Number(loadedbytes)/Number(timeelapsed)/1000);
    yeah i wanna round that ^^^^^ off to 2 decimal places instead of 6 million decimal places.

    thanx a million in advance.

  2. #2
    Senior Member
    Join Date
    Apr 2000
    Location
    Northern Ireland
    Posts
    2,146
    Looks like there is no straight-forward way, but here's something pretty simple you can try ...

    Code:
    temp = (Number(loadedbytes)/Number(timeelapsed)/1024);
    kbs = Math.round(temp * 100) / 100
    To begin with, there are 1024 bytes in a kilobyte, not 1000 as you had assumed. Anyways, the 'temp' variable gets the precise Kbs, and the 'Kbs' variable simply multiplies that number by 100, rounds it to an integer, then divides it by 100, giving it 2 decimal places.

    BTW, I found this solution in Colin Moock's 'Actionscript: The Definitive Guide' - it's a smashing book. Credit where credit is due

  3. #3
    Thanx a million. I know that there are 1024 bytes in a kilobyte, but i dont know why i put down a 1000?
    oh well the method worked.

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