Page 1 of 1

Script functions trunc and frac

Posted: 26 Oct 2015 14:46
by AdemirMartins
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

Re: Script functions trunc and frac

Posted: 26 Oct 2015 19:54
by Martin
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

Re: Script functions trunc and frac

Posted: 28 Oct 2015 01:19
by AdemirMartins
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

Re: Script functions trunc and frac

Posted: 29 Oct 2015 19:48
by Martin
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