Having to cast slider value to float in order to round it

Hi,

I’ve been playing with the “new to me” label parameter of the Slider COMP trying to get it to display the slider’s current value. While this worked on the first attempt:

'VALUE: ' + str(me.par.value0)

This resulted in an error when I tried to round the value to two decimals:

'VALUE ' + str( round( me.par.value0, 2) )
TypeError: type td.Par doesn't define __round__ method

I had to explicitely cast the slider’s value0 parameter to a float in order for the second argument of the round method to be taken into account:

'VALUE ' + str( round( float(me.par.value0), 2)

I understand that value0’s type is in fact td.Par while the round method is probably expecting a float but why is the cast not necessary in the first case but required in the second?

Thanks!

The reason is because str() is a cast operation, which we implement for td.Par. There are other cases where we auto-cast td.Par to float and other things to make things a little less verbose, but it’s easy to run into cases where the auto-cast isn’t going to be used, requiring you to write more complete code.

Makes sense. Thanks Malcolm!