String to Integer

Why is the above so difficult???

def myStr="7.123454565"
state.myInt=Integer.parseInt(myStr,16) // Exception error
state.myInt=myStr.toInteger() // Same
state.myInt=myStr as Integer // Dont think this even compiles.

Tried a whole bunch more and none work. Surely, I must be missing something as I cant believe such conversion is not possible.:rage:

Your string isn’t an integer (whole number). I think you would have to parse it into a double or float first, then round and finally convert that to an integer. If you parse to a double or float, then convert to an integer without rounding, it will just throw away everything after the decimal.

I despise type conversions!

1 Like

That did the trick :smile:

def dDist = Math.round(Double.parseDouble(dist[2]))
state.distance = dDist.toInteger()