Python Math Problem

Posted on
Mon Jun 06, 2022 8:37 am
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Python Math Problem

Years ago I got a lot of help with a script to covert rain totals in MM to Inches. This has worked just fine but now with the Python 3 update, I've got a small problem. Here is the code:
Code: Select all
rain = indigo.variables[1938961] #rain in mm
inches = "{:.2f}".format(float(rain.value) *.03937) #coverts mm to inches
indigo.variable.updateValue(1852577407, inches) #inserts inches into variable

and my only issue is now the calculation is giving me too many decimal places. I think the
Code: Select all
"{:.2f}"
was taking care of this, but no longer. I'm pretty sure this happened with the Python 3 update, but am not 100% sure. I've looked at this link
https://python.shiksha/tips/limiting-float-upto-2-places/
but can't figure out where some of this should go in my equation. I even tried reformatting the calculation result before it is written to the variable, but again, had no luck, so I'm hoping there is a way to limit the calculation to just 2 decimal places.

Posted on
Mon Jun 06, 2022 9:26 am
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Math Problem

That format string is working properly for me.

Maybe a screen shot of the variables?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Jun 06, 2022 9:46 am
jay (support) offline
Site Admin
User avatar
Posts: 18212
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Python Math Problem

I also tried it and it works:

Code: Select all
>>> rain = indigo.variables[534204190]
>>> print(rain.value)
5.582394
>>> inches = "{:.2f}".format(float(rain.value) *.03937) #coverts mm to inches
>>> print(inches)
0.22
>>> indigo.variable.updateValue(595336786, inches)
>>> rain_inches = indigo.variables[595336786] # "inches_var"
>>> print(rain_inches.value)
0.22


So I'm unclear on where it's going wrong. I'd recommend doing prints in between the steps to help isolate where the problem might be.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jun 06, 2022 10:42 am
jltnol offline
Posts: 994
Joined: Oct 15, 2013

Re: Python Math Problem

Ok so looks like something else may be wrong... but am I right in assuming the
"{:.2f}"
limits the output to 2 decimal places?

Posted on
Mon Jun 06, 2022 10:45 am
FlyingDiver offline
User avatar
Posts: 7210
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Python Math Problem

jltnol wrote:
Ok so looks like something else may be wrong... but am I right in assuming the
"{:.2f}"
limits the output to 2 decimal places?


Yes

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests