frame

round(number[, ndigits]) ?

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.

Comments

  • 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 * 10**n
        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).

    Here you can find an online tool to run some tests.

    Let me know if this can help you :)
    Matteo Cipriani
    Zerynth Support Team
Sign In or Register to comment.

ZERYNTH Community

@ 2016 Zerynth.com, all rights reserved.