round(number[, ndigits]) ?


#1

I need to round an (x) to n decimal places. I tried using: round(x, 3)
but it seems like this isn’t included in Zerynth? It gives me [error] Can’t find name [round] 
using formatting:  “%.3f” % ] x  
that works, but that isn’t a solution for my problem as it will just format the answer not actually round it.


#2

Hi potato,

We have not ported the Python3 built-in round() function in Zerynth Built-in functions but should be available soon in one of our future releases.

In the meantime, you can use this few lines of code and obtain the same result:

def round(value,n=0):
value_m = value * 10n
value_int = int(value_m)
if value >= 0:
if value_m-value_int >= 0.5:
value_int += 1
else:
if value_m-value_int <= -0.5:
value_int -= 1
return value_int/(10
n)
Pay attention to the issue related to the 32-bit float representation ([cit.] Not every decimal number can be expressed exactly as a floating point number. This can be seen when entering "0.1" and examining its binary representation which is either slightly smaller or larger, depending on the last bit).<br><br><a href="https://www.h-schmidt.net/FloatConverter/IEEE754.html">Here</a> you can find an online tool to run some tests.<br><br>Let me know if this can help you :)