Round a variable

About advanced scenes, and the Thyme scripting language used in Algodoo.

Round a variable

Postby KarateBrot » Wed Sep 09, 2009 10:41 pm

How can I round a variable? Does someone know a function for it that works in Algodoo?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Round a variable

Postby Ian151 » Thu Sep 10, 2009 12:07 am

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
"THE BEST 2 PLAYER SCENE EVER MADE!!!" - ianno
http://www.algodoo.com/algobox/details.php?id=24204
Image
User avatar
Ian151
 
Posts: 47
Joined: Tue Sep 01, 2009 12:02 am

Re: Round a variable

Postby KarateBrot » Thu Sep 10, 2009 12:55 am

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
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Round a variable

Postby standardtoaster » Thu Sep 10, 2009 1:35 am

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. ;)
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Round a variable

Postby KarateBrot » Thu Sep 10, 2009 1:46 am

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?
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Round a variable

Postby link0007 » Thu Sep 10, 2009 8:19 am

Luckily for you, some very smart people (*cough* me *cough*) have worked on that matter:

http://www.phunland.com/forum/viewtopic.php?id=6972
Link: "Surely somebody hates Walter Cronkite.."
Sonic: "Probably.. But somebody hates everyone."
:D
User avatar
link0007
 
Posts: 408
Joined: Thu Jun 11, 2009 2:45 pm

Re: Round a variable

Postby RA2lover » Thu Sep 10, 2009 6:24 pm

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)
Jrv wrote:
TC42 wrote:Quite honestly, I didn't think anyone on 4chan has that good a use of grammar, spelling, usage, mechanics, ect.
But I've never been there, so I may be wrong.


GTFO newfgt
User avatar
RA2lover
 
Posts: 607
Joined: Mon Aug 31, 2009 8:43 pm
Location: Brazil

Re: Round a variable

Postby KarateBrot » Fri Sep 11, 2009 12:45 am

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
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests

cron