-0.00 not equal 0.00

I have a problem rounding numbers to zero. I’ve written a python script to produce g code, the moves are modal so only get output when they change the trouble is sometimes I get a -0.00 like in this example. Ypt shows -0.00

Is there anyway to round the numbers so I don’t get -0.

Mark.

with a quick google search i found this:


a = -0.000
b = format(a if a != 0 else abs(a))
print  b

is its -1.00 it will still be -1.00, if its 1.00 it will stay 1.00 but -0.00 will be 0.00

1 Like

Dunno, you might just need to run the numbers through a filter before formatting like:

import Rhino.RhinoMath as rm
if rm.EpsilonEquals(num_to_check,0,rm.SqrtEpsilon): num_to_check=0
#can substitute something other than SqrtEpsilon for tolerance level

–Mitch

Thanks for the pointers. I came up with this function that seem to work.

def RoundCoodinate(n, p = 3):
    """Rounds number to precision and returns a string
    Parameters:
    n = Number to round
    p = 3 Precision Optional
    """
    P = '{:.' + str(p) + 'f}'

    n = round(n, p)
    return(P.format(n if n != 0 else abs(n)))

Mark