Decimal Precision


I haven’t received my Tidbyt yet so I figured I’d kill some time coding a couple of rudimentary apps. I am struggling with decimal precision in Pixlet/Starlark. I’ve combed through the Starlark spec, and docs on Github for pixlet and I can’t find how to accomplish this task. Any help is appreciated!

According to the starlark specification, a float is an IEEE 754 double-precision floating point number and an int is a signed integer of arbitrary magnitude.

1 Like

Thanks for the reply. Maybe I am missing something, but how would I round to 2 decimal places for a float?

You’d need to load the math library and do something like math.round(value * 100) / 100. Since it’s IEEE 754, the fraction is encoded in binary which will leave a small error with respect to decimal, so if you want to print this nicely, you have to do some funky stuff because of starlark’s limited formatting support, something silly like

v = str(int(math.round(89.955 * 100)))
v = (v[0:-2] + "." + v[-2:])    

Worked like a charm! Thank you so much!