Page 1 of 1

Simple Rounding Question

PostPosted: Mon Jun 10, 2019 8:27 pm
by jltnol
I'm keeping tabs on something that outputs in seconds... but there are thousands of them, so the number is meaningless. I'm trying to get the seconds converted to hours, which I've been able to do, but would like to keep the answer to 2 decimal places.

Here is my original code:
Code: Select all
theDifa =str(float(theVar1.value) /3600) #seconds into hours
indigo.variable.updateValue(1924530586, ((theDifa)))

and this is what I get:
7.53694444444

and here's ONE of the things I've tried, but of course, it doesn't work:
Code: Select all
theDifa =str(float(theVar1.value) /3600)   #seconds into hours
answer = str(round(theDifa, 2))
indigo.variable.updateValue(1924530586, ((theDifaR1)))


So basically, I'm taking the value of theVar1, divide by 3600 (seconds in an hour), then truncate the answer to 2 decimal places, and then put it back into a Indigo Variable. Do I need to create a new variable to be the x,2 number, and then put that back into the Indigo Variable ?

Re: Simple Rounding Question

PostPosted: Mon Jun 10, 2019 8:37 pm
by FlyingDiver
Code: Select all
theDifa = "{:.2f}".format(theVar1.value /3600.0)
indigo.variable.updateValue(1924530586, theDifa)
 

Re: Simple Rounding Question

PostPosted: Mon Jun 10, 2019 8:51 pm
by jltnol
Hey Joe

Thanks for your help, the code passes the compiler, but I'm getting an error message:

Code: Select all
Script Error                    embedded script: unsupported operand type(s) for /: 'unicode' and 'float'
   Script Error                    Exception Traceback (most recent call shown last):

     embedded script, line 14, at top level
TypeError: unsupported operand type(s) for /: 'unicode' and 'float'


I'm totally in the dark here as to what the error means, much less how to fix it.

Thanks

Re: Simple Rounding Question

PostPosted: Mon Jun 10, 2019 8:53 pm
by FlyingDiver
I think this works:

Code: Select all
theDifa = "{:.2f}".format(theVar1.floatValue /3600.0)
indigo.variable.updateValue(1924530586, theDifa)


If not, this will:

Code: Select all
theDifa = "{:.2f}".format(float(theVar1.value) /3600.0)
indigo.variable.updateValue(1924530586, theDifa)

Re: Simple Rounding Question

PostPosted: Mon Jun 10, 2019 8:54 pm
by FlyingDiver
jltnol wrote:
I'm totally in the dark here as to what the error means, much less how to fix it.


Means that theVar1.value was returning a unicode string, not a float. My bad.

Re: Simple Rounding Question

PostPosted: Mon Jun 10, 2019 9:05 pm
by jltnol
The 2nd one works perfectly

Thanks again for your help. :D