Script functions trunc and frac

Post your feature requets for new triggers, conditions, actions and other improvements.

Moderator: Martin

Locked
AdemirMartins
Posts: 2
Joined: 26 Oct 2015 14:30

Script functions trunc and frac

Post by AdemirMartins » 26 Oct 2015 14:46

Martin,

Beautiful your work. Thanks a lot for your app.

Performing some calculations on a script I missed two simple functions:
- Int trunc (value): Returns only the integer part with his sign of a number
- Float frac (value): Returns only the fractional part of a number

Sorry if there is already some way to get it, but I searched and could not find it.

Ademir

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Script functions trunc and frac

Post by Martin » 26 Oct 2015 19:54

Hi,

Thanks, glad you like Automagic!

You could use function floor, ceil or round to get the nearest integer value, technically it's still a floating point number so will display as XYZ.0.
v=1.24;
a=floor(v);

If you want to get the integer part formatted without decimal places I recommend to use an inline script to define the format you need:
formatted_without_decimal_places = "{a,numberformat,0}";

Regards,
Martin

AdemirMartins
Posts: 2
Joined: 26 Oct 2015 14:30

Re: Script functions trunc and frac

Post by AdemirMartins » 28 Oct 2015 01:19

Hi,

I did some testing and found that the function round gives me the result I want.
Different than the definition says (that it rounds), it is only truncating... exactly what I need.
For example, both 3.1 and 3.9, results in pure 3.0

Thanks,

Ademir

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Script functions trunc and frac

Post by Martin » 29 Oct 2015 19:48

Hi,

Are you sure that round truncates the value? On my test devices 3.9 is rounded to 4.0. Could you share your script so I can test it on my device?

Regards,
Martin

Locked