Page 1 of 1

Round a variable

PostPosted: Wed Sep 09, 2009 10:41 pm
by KarateBrot
How can I round a variable? Does someone know a function for it that works in Algodoo?

Re: Round a variable

PostPosted: Thu Sep 10, 2009 12:07 am
by Ian151
Suprisingly I have a good answer for this. You see, there seems to be no rounding command I know of, so I created an if statement to replace one. This user created command asks if a decimal has the number after the point to be 5 or higher, if it is, then it rounds up, if not, then it rounds down.

Example:
5.74 => 6
10.333333 => 10
3.5 => 4


put this in oncollide, otherwise find a way to translate it to a console code.

Code: Select all
scene.my.number >= (math.toint(scene.my.number) + 0.5) ? {scene.my.number = math.toint(scene.my.number) + 1} : {};     scene.my.number < (math.toint(scene.my.number) + 0.5) ? {scene.my.number = math.toint(scene.my.number)} : {}


the only thing you must replace in that code is scene.my.number, which is the number you want to use.

I used mostly if statement and Math.toint, which changes a decimal to an integer, but only to the highest point of the former decimal, from 10.9 to 10, for example.

If that is not exactly what you want, then explain.

If it is, then I'm glad I helped you :D

Re: Round a variable

PostPosted: Thu Sep 10, 2009 12:55 am
by KarateBrot
yeah thanks that's nearly what i want! but is it possible to round to specific decimal places?

for example 6.490843697878903 will be rounded to 6.49084

Re: Round a variable

PostPosted: Thu Sep 10, 2009 1:35 am
by standardtoaster
KarateBrot wrote:yeah thanks that's nearly what i want! but is it possible to round to specific decimal places?

for example 6.490843697878903 will be rounded to 6.49084

To do that we would need to be able to truncate. ;)

Re: Round a variable

PostPosted: Thu Sep 10, 2009 1:46 am
by KarateBrot
standardtoaster wrote:
KarateBrot wrote:yeah thanks that's nearly what i want! but is it possible to round to specific decimal places?

for example 6.490843697878903 will be rounded to 6.49084

To do that we would need to be able to truncate. ;)


but isn't it possible to change Ian's code a bit so that the number first is multiplied by for example 100000, then be rounded to the whole number and after that divided by 100000. That should work or what do you think of this idea?

Re: Round a variable

PostPosted: Thu Sep 10, 2009 8:19 am
by link0007
Luckily for you, some very smart people (*cough* me *cough*) have worked on that matter:

http://www.phunland.com/forum/viewtopic.php?id=6972

Re: Round a variable

PostPosted: Thu Sep 10, 2009 6:24 pm
by RA2lover
just use number - math.mod(number, 0.0(you choose the numbers you want to round)1 )

i made an old example scene...
http://www.algodoo.com/algobox/details.php?id=25798

degrees are rounded in 0.5 increments(not enough text space)

Re: Round a variable

PostPosted: Fri Sep 11, 2009 12:45 am
by KarateBrot
thanks.

@link
i got a very similar idea today in school while nearly sleeping. just was sittign around, bored, and suddenly BAM it came to my mind :D
I instantly wrote down my ideas and made a code out of it. it's a similar method of your code.

- - - - -

If anyone is interested that's my code:

Code: Select all
Scene.my.round = (x, digits) => { ( x*10^(digits + 1) - math.mod(x*10^(digits + 1), 1) ) >= ( (x*10^digits - math.mod(x*10^digits, 1) ) * 10 + 5 ) ? { ( (x*10^digits - math.mod(x*10^digits, 1)) + 1 ) / 10^digits } : { (x*10^digits - math.mod(x*10^digits, 1)) / 10^digits } }


x is your variable or figure and digits stands for the desired decimal places