 00:00
00:00 ## AS: Rounding (to the nearest)

5,455 Views | 22 Replies

AS: Rounding (to the nearest) 2006-08-02 01:31:27

AS: MAIN

AS: ROUNDING
By Snubby

What will I learn?

You will learn everything to know there is to know about rounding, including...

- Simple rounding

- The Math class
- Different types of rounding
- Rounding to the nearest 1, 2, 3, 4, 5, 10, 20... etc.
- The uses of rounding.

And so on!

Pff, I already know how to round, thats noooooob...

Yeah hot shot but do you know how to round to the nearest ten? huh? I didn't think so...

BASIC:

What is rounding? Rounding is taking a decimal number and increasing it to the nearest whole number. example.

0.7 to 1.

Rounding comes in handy when you want to show a number without a bazillion decimal places after it, like a preloader percentage.

Here are the different forms of rounding...

Math.round();
Math.floor();
Math.ceil();

Each form takes one argument (something between brackets), the number you want to round.

Math.round(); // Round a number either up or down. from 0-4 rounds down, and 5-9 rounds up.
Math.floor(); // Round a number down.
Math.ceil(); // Round a number up.

Here is a line of code that includes rounding you might see in a script...

percentage = Math.round(getBytesTotal() / getBytesLoaded() * 100);

that could act as a preloader variable.

INTERMEDIATE

When using rounding, you can combo it with other Math, like random(). If you want to go to a random frame from 1 - 10, you can use rounding and random (because there is no frame 5.7, you have to go to frame 6).

gotoAndStop(Math.round(Math.random() * 9 + 1));

The random creates a random number from 0 - 1, like 0.943753. I then multiply that by 9, giving a number from 0-9. Then I add one, giving a number from 1-10.

Say I want a number from one to ten. I was working on a pixel game engine where I wanted to have a charged up laser, that as you held the mouse the laser got bigger to the nearest 10 pixels. Wanna see?

Example
The Code // remember, its not API, I've got crap in the library

So how do me do it? Well we use a combination of rounding, dividing and multiplying. Heres my script for a number to the nearest 10.

var myNum:Number = 6.9384;

So, this creates a numerical variable, myNum, and sets it to 6.9384.

var myRoundNum:Number = Math.round(myNum / 10) * 10;

This takes myNum, and rounds it to the nearest 10. How?

Math.round(myNum / 10);

so this divides 6.9384 by ten, which is 0.69384 (but who cares?) and then rounds that, which makes one. That then is multiplied by ten, which gives 1 * 10 = 10!!

If I wanted to round to the nearest 5, i could go

var myRoundNum:Number = Math.round(myNum / 5) * 5;

see?

- - - - - - - - - -

I'm open to comments, though I think the rounding to the nearest is probably the only usefull part of this :P

Response to AS: Rounding (to the nearest) 2006-08-02 01:34:06

Very good, this is a very clear tutorial actually, I'm testing some code with this to see if that improves the performance of my game. Great work!

Response to AS: Rounding (to the nearest) 2006-08-02 01:37:56

At 8/2/06 01:34 AM, JamsCo wrote: Very good, this is a very clear tutorial actually, I'm testing some code with this to see if that improves the performance of my game. Great work!

thanks! I was actually expecting a bunch of morons saying, 'err this has been done before'. Thanks a lot!

Response to AS: Rounding (to the nearest) 2006-08-02 01:47:14

*Applauds Snubby* Great tut Snubby. I expected nothing less from you. I am sure I'll get a chance to use this. Thanks

Response to AS: Rounding (to the nearest) 2006-08-02 03:42:51

int() although, from AS3 onwards, is now the constructor for the 'int' datatype is another function you can use to round numbers

int, simple removes the decimal part of the number, without any specific rounding, for positive numbers, it acts as floor(), and for negative numbers, it acts as ceil()

and to expand a little on floor and ceil
you said round down, and round up, but what exactly do you mean by that? its not very clear

a more precise, clear definition would be

Math.floor() rounds to the nearest number, less than or equal to it
Math.ceil() rounds to the nearest number, more than or equal to it

Math.floor(0.8), nearest number is 1, but it has to be less than or equal to it, 0
Math.foor(-0.2), nearest number is 0, but it has to be less than or equal to it, -1

Math.ceil(0.2), nearest number is 0, but it has to be more than or equal to it, 1
Math.ceil(-0.7) nearest number is -1, but it has to be more than or equal to it, 0

another thing you can use in flash, pre AS3, is the deprecated random() function, a global function that takes an integer input, and returns an integer from 0 to the input-1

for example

random(10) would return a number 0 to 9 inclusive

random(n+1) is alot faster than using Math.round(Math.random()*n) Response to AS: Rounding (to the nearest) 2006-08-02 09:05:56

At 8/2/06 03:42 AM, -dELta- wrote: Stuffness. OMG!

Delta is right, but its still a good tutorial, not terribly advanced, but not a begginers thing either, it shoudl go in intermediate.
Nice tutorial though :) Response to AS: Rounding (to the nearest) 2006-08-02 11:24:25

Thanks everybody, and delta, whatever you are doing it looks right, so have fun with it, your way is probably better :D

Response to AS: Rounding (to the nearest) 2006-08-13 20:31:50

At 8/2/06 03:42 AM, -dELta- wrote: Math.floor(0.8), nearest number is 1, but it has to be less than or equal to it, 0
Math.foor(-0.2), nearest number is 0, but it has to be less than or equal to it, -1

Math.ceil(0.2), nearest number is 0, but it has to be more than or equal to it, 1
Math.ceil(-0.7) nearest number is -1, but it has to be more than or equal to it, 0

so
Math.ceil(1) is 0??
and
Math.floor(0) is 1??

I didnt know that
lol

Response to AS: Rounding (to the nearest) 2006-08-13 21:17:55

and heres a usful function im wiriting on the spot (not gonna test it coz its simple, but i could make typo)

function round(number, nearest) {
nearest == undefined ? nearest=1 : 0;
nearest>0 ? nearest=1/(Math.pow(10, -nearest)) : nearest=Math.pow(10, nearest);
number = Math.round(number*nearest);
return number/nearest;
}

use:

round( number, nearest ), number is the value to round, nearest unit from decimal point.
e.g.

round( 19.687 , 2 ) = 19.69

or

round ( 25543.698, -4 ) = 30000

Response to AS: Rounding (to the nearest) 2006-08-13 21:19:47

Math.ceil(1) is 1
and
Math.floor(0) is 0

Response to AS: Rounding (to the nearest) 2006-08-13 21:20:57

At 8/13/06 08:31 PM, phyconinja wrote: Math.ceil(1) is 0??
Math.floor(0) is 1??

no... hes commenting on the difference between int and floor

int removes everything after the decimal point (datatypes as integer)
floor goes to the closest integer less than the parameter specified. Response to AS: Rounding (to the nearest) 2006-08-13 21:24:11

i dont know wat your on about, but int or not, under any circumstances u want...

Math.ceil(1) is 1
and
Math.floor(0) is 0

Response to AS: Rounding (to the nearest) 2006-08-13 23:06:51

At 8/13/06 09:20 PM, authorblues wrote: int removes everything after the decimal point (datatypes as integer)
floor goes to the closest integer less than the parameter specified.

Technically, no it doesn't. It only makes it invisible, the numbers after the decimals still exist in memory storage. That's why it's possible to typecast something to int to determine how many decimal places to display, such as....

Say you have a variable called otherVariable that holds the value of 10.025758902309342

If you do..

variable = int(otherVariable*100)/100, it will display 10.02

If int completely removed the numbers after the decimal point... how would this be possible? Response to AS: Rounding (to the nearest) 2006-08-13 23:22:12

At 8/13/06 11:06 PM, Johnny_Krysys wrote: variable = int(otherVariable*100)/100, it will display 10.02
If int completely removed the numbers after the decimal point... how would this be possible?

lets see, maybe because if you break it apart...

otherVariable = 10.02198723491238740971023874
v = int(otherVariable*100); // v = 1002
variable = v/100; // variable = 10.02 Response to AS: Rounding (to the nearest) 2006-08-13 23:23:47

At 8/13/06 09:24 PM, xWELSHxDRAGONx wrote: i dont know wat your on about, but int or not, under any circumstances u want...

i dont even know why people keep mentioning this. that is obvious...
if you round (round, floor, ceil, int) a number that is already an integer, nothing changes Response to AS: Rounding (to the nearest) 2006-08-14 03:15:11

At 8/13/06 11:22 PM, authorblues wrote: lets see, maybe because if you break it apart...

otherVariable = 10.02198723491238740971023874
v = int(otherVariable*100); // v = 1002
variable = v/100; // variable = 10.02

Oh, I know. I know you know. I didn't want anyone that didn't know to think that if you cast something to int, everything after the decimal place is completely erased (which it seems happens if you trace() it. In the above example, I didn't want anyone thinking that once you cast it to int, the only that that is remaining is 10. Not so much correcting you as elaborating on it. Sorry if it came out condesending. I know you know your shit, blues. Response to AS: Rounding (to the nearest) 2006-08-14 04:17:29

At 8/14/06 03:15 AM, Johnny_Krysys wrote: Oh, I know. I know you know. I didn't want anyone that didn't know to think that if you cast something to int, everything after the decimal place is completely erased (which it seems happens if you trace() it. In the above example, I didn't want anyone thinking that once you cast it to int, the only that that is remaining is 10. Not so much correcting you as elaborating on it. Sorry if it came out condesending. I know you know your shit, blues.

but it IS removing the decimals. that is how flash handles the difference between a float and an int. flash tries to handle all numbers as floats. in your example, you WERE losing the precision of the number, because you were casting into the integer scope (truncating decimals) on a number you were going to end up dividing, just to get those decimals back.

flash does not keep those decimals in memory, if that were true:

var n:Number = 10.213213213213;
trace(int(n)*100); // output: 1021

but what really happens is that it will output 1000... Response to AS: Rounding (to the nearest) 2006-08-14 07:24:28

I don't notice it's you with that sig.

Response to AS: Rounding (to the nearest) 2006-08-14 16:32:23

At 8/14/06 04:17 AM, authorblues wrote: but it IS removing the decimals. that is how flash handles the difference between a float and an int. flash tries to handle all numbers as floats. in your example, you WERE losing the precision of the number, because you were casting into the integer scope (truncating decimals) on a number you were going to end up dividing, just to get those decimals back.

flash does not keep those decimals in memory, if that were true:

var n:Number = 10.213213213213;
trace(int(n)*100); // output: 1021

but what really happens is that it will output 1000...

"Note : This is due to the fact that the int function does not erase the decimals; it just does not show them. "

Taken from.

So now, I'm confused. Please enlighten me so I stop making an ass out of myself. Response to AS: Rounding (to the nearest) 2006-08-14 16:58:59

I just want to say that there is a very minor problem with this line of code if anyone wants events to be as close to totally random as possible.

gotoAndStop(Math.round(Math.random() * 9 + 1));

In that code, the numbers 1 and 10 have only one half the chance of being generated as any other number. The reason is this: Math.random()*9 + 1 can only generate a number between 1.0 and 10.0. For Math.round() to bring the value to 1, the value of Math.random()*9+1 must be between 1.0 and 1.5. For 10, it must be between 9.5 and 10. Notice that each only has a span of .5 where they will turn up. But in order to get a 2, you can make an unrounded random number anywhere between 1.5 and 2.5. That's a span of 1, not .5. So therefore, 1 and 10 only have half the chance of turning up when you use the Math.round() method. There is an alternative though:

Math.floor(Math.random()*10+1);

Now, the span for each number is the same, in order to get a 1, Math.random()*10+1 must generate between 1 and 2, for 10, it must generate between 10 and 11. Every number has a span of 1 so there is an equal chance for each number.

I hope that helped somebody. If it just annoyed everyone then I apologize.

Response to AS: Rounding (to the nearest) 2006-08-15 03:43:43

At 8/14/06 04:32 PM, Johnny_Krysys wrote: So now, I'm confused. Please enlighten me so I stop making an ass out of myself.

ill try to document their documentation to show where they messed up...

value= int((20/7)*100)/100;

a reasonable function for truncating to two decimal places

int(20/7) returns the value of 2 by removing the decimal places off the number created by the calculation in parentheses so that it appears as a whole number.

the term for this is truncation. split the number at the decimal point, return everything before.

big error ALERT: the documentation here examines the value of int(20/7)... but in the function in question, it is int((20/7)*100), which is a completely different situation. in this case, those decimal points are saved because you are changing the location of the decimal.

by the errors in this document, they are attempting to state the following:
int((20/7)*100)/100 == (int(20/7)*100)/100 == int(20/7)
which is a logical fallacy. you have the right idea, but the documentation is all wrong.

i honestly do not think that flash handles integer casting. as a matter of fact, the number system in flash is quite basic and full of errors. case in point:

trace(Math.sin(Math.PI)); // sin(PI) == 0

anyone who has taken a trig course expects this to output 0. but instead youll get:
1.22460635382238e-16, which is terribly close to 0, but it would ruin any tests like:

if (Math.sin(someVariable)==0) // do something
where, you are expecting it to evaluate true when someVariable == PI. Response to AS: Rounding (to the nearest) 2006-08-15 04:02:13

At 8/2/06 01:31 AM, Snubby wrote: Example

Good tut, but I dont think that the example woked right. I had to keep klicking it over and over again.

<3 Response to AS: Rounding (to the nearest) 2006-08-15 16:33:27

At 8/15/06 03:43 AM, authorblues wrote: big error ALERT: the documentation here examines the value of int(20/7)... but in the function in question, it is int((20/7)*100), which is a completely different situation. in this case, those decimal points are saved because you are changing the location of the decimal.

Ohhhh. I see the difference now. Before anything is even cast to int, it is multiplied by 100, ...then int is applied. So in the case of something like 22/7, istead of getting 3.14, you're getting 314.blahblah before the int function is applied.

Got it.

anyone who has taken a trig course expects this to output 0. but instead youll get:
1.22460635382238e-16, which is terribly close to 0, but it would ruin any tests like:

This actually explains a lot. I took a trig course 2 semesters ago, and decided to write simple flash programs to handle problems because I didn't have a scientific calculator and didn't want to do them longhand. Nothing was coming even close in flash, compared to my longhand.

Thanks author 