A Flash Developer Resource Site

1. rounding numbers...

Alright I know how to round a number, but ... I don't know what else to call this.

I get a number simular to 308,916.82 ... I don't have a problen doing th math to round that up ... Math.ceil ... my problem is making that number flat to the next position down like: 310,000. So if I had 1152 i'd want it to be 1200. If I had 4,500,345 I'd want 4,600,000. Make sense?

I hope so ... can anyone help me with this?

2. First off, to round a number up to the next 100, you would do this:

n = Math.ceil(n/100)*100;

In your case, however, you don't want to round to 100. In the case of 1152, you want to round to the next 100. But in the case of 4,500,345, you want to round to the next 100,000.

You can use the Math.log() function to find the appropriate power of 10 for the number you are rounding.

To find the logarithm, base 10 of a number N, you use:

power10 = Math.log(n)/Math.log(10);

In your case, you appear to be using this:

power10 = Math.floor(Math.log(n)/Math.log(10))-1;

If you then raise 10 to that power, you'll get the rounding number you desire.

roundScale = Math.pow(10, power10);

Here's the code:

code:
```
function  myRoundUp(n)
{
// next highest power of 10 for this number
power10 = Math.floor(Math.log(n)/Math.log(10))-1;
// 10 ^ power10
roundScale = Math.pow(10,power10);
trace('roundScale = ' + roundScale);
n = Math.ceil(n/roundScale) * roundScale;
return n;
}

trace(  1152 +": " + myRoundUp( 1152 ));
trace(  4500345 +": " + myRoundUp( 4500345 ));
```

This produces the desired result.

3. thanks a ton ... ... all that will help me a ton in the future!

Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap