Rounding time to one or two decimals?

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

Rounding time to one or two decimals?

Postby McCool1979 » Thu Sep 03, 2015 2:23 pm

Hi guys!

I have a little scene that has a running stopwatch on screen. The start/stop is triggered by onLaserHit and uses sim.time. The time is shown continuously on screen (just like in sporting events) in a box via the text parameter.

Is there a way to round the output on screen to one or two decimals to make it look a lot better visually? A hack like just plopping an extra box on top to hide anything beyond one or two decimals is not a good solution for me.

Edit: It does not need to be mathematically correct, so if it is rounded or just limited to showing one or two deicmals does not matter to me.

Example: 29.541668 should be shown as 29.5 or 29.54 (I am not sure if I am going to be using one or two decimals yet). It would also be very useful if exact values with no or one decimal such as 22 or 22.5 are shown using two decimals - like 22.00 or 22.50.

I am not much of a coder, so I hope you could give me some input to this (probably) easy fixable problem.

PS: Being new here I have searched extensively and looked at lots of scenes to try to find a solution before posting this question, but everything I have come across so far uses either the full value or rounds to whole numbers.
McCool1979
 
Posts: 2
Joined: Thu Sep 03, 2015 1:48 pm

Re: Rounding time to one or two decimals?

Postby homieeee » Thu Sep 03, 2015 7:05 pm

_round will take in a floating point number and will remove excess decimals.

i.e => _round(10.1234, 0) => 10
i.e => _round(10.1234, 1) => 10.1
i.e => _round(10.1234, 2) => 10.12

Any other questions just ask.

Code: Select all
_round= (float, decimals) => { math.toint(float * (10^decimals))/10^decimals}
User avatar
homieeee
 
Posts: 14
Joined: Thu Oct 09, 2014 2:13 am

Re: Rounding time to one or two decimals?

Postby McCool1979 » Mon Sep 07, 2015 11:15 pm

This worked very well - thank you!

Would have gotten back to you a bit earlier, but my SSD crashed so I was left without a working computer (oh, the horror!) for a few days.
McCool1979
 
Posts: 2
Joined: Thu Sep 03, 2015 1:48 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 7 guests