Claus_Jambrich_5663

Rounding of floats

Discussion created by Claus_Jambrich_5663 on Apr 3, 2017
The arithmetic functions in Automic only deliver basic mathematical functions. E.g. no round function is available. Here is a short script that can be used e.g. in an include. Create a new include named e.g. JOBI.ROUND with the following lines:

!Definition of needed variables
:DEFINE &var1#, float
:DEFINE &result1#, float
:set &i# = 0
:set &j# = 1
:set &var1# = 5
:SET &outform# = "0."

!Maximum precision is 10
:IF &digits# > 10
:  SET &digits# = 10
:ENDIF

:SET &digits1# = &digits# + 1

:WHILE &i# < &digits1#
:  SET &var1# = &var1# * 0.1
:  SET &i# = &i# + 1
:ENDWHILE

:WHILE &j# < &digits1#
:  SET &outform# = "&outform#0"
:  SET &j# = &j# + 1
:ENDWHILE

!Is the result of the subtraction negative or positive
:IF &toround# < 0
:  SET &result1# = sub(&toround#,&var1#)
:ELSE
:  SET &result1# = add(&toround#,&var1#)
:ENDIF

!Format result
:SET &end_result# = FORMAT(&result1#, &outform#)

The include is e.g. called like that:

:DEFINE &toround#, float
:SET &digits# = 8
:SET &toround# = SUB(12.334554354354,9.54684655)

:INCLUDE JOBI.ROUND

:PRINT "Result &end_result#"

The variable &toround# is the number to be rounded and &end_result# is the rounded result. &digits# sets the number of digits behind the decimal point. The precision is limited to maximal 10 characters.
Special thanks to Rene_Kappel_161 for his valuable input.

Outcomes