Temperature Conversion

Posted on
Sat Apr 20, 2024 1:26 am
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Temperature Conversion

I would appreciate some help with getting this Python code correct. The following code seems to work, except I’m not getting consistent results with decimal places.
Code: Select all
# Conversion ºC to ºF
celsiusVar = indigo.variables[123456789]
tempVar = celsiusVar.getValue(float) 
indigo.variable.updateValue(987654321,str(tempVar * 9/5 + 32))

Sometimes the result might be xx.xx (2 decimal places), and sometimes it might be something like this (xx.xxxxxxxxx) both from the same code.

I would like to see examples of how this code can be written to control decimals as follows:

No decimal (xx)
1 decimal (xx.x)
2 decimals (xx.xx)

Also, do I need a “default=xx” after float? .getValue(float, default=xx)

Posted on
Sat Apr 20, 2024 2:30 am
CliveS offline
Posts: 771
Joined: Jan 10, 2016
Location: Medomsley, County Durham, UK

Re: Temperature Conversion

I copied your question verbatim into my chatGPT and got the following reply.

To control the number of decimal places in your temperature conversion output, you can format the result using Python's string formatting capabilities. Here's how you can modify your code to achieve the desired number of decimal places:

Code: Select all
# Conversion ºC to ºF
celsiusVar = indigo.variables[123456789]
tempVar = celsiusVar.getValue(float)

# Format the temperature with different decimal places
temp_no_decimals = f"{tempVar * 9/5 + 32:.0f}"  # No decimal places
temp_one_decimal = f"{tempVar * 9/5 + 32:.1f}"  # One decimal place
temp_two_decimals = f"{tempVar * 9/5 + 32:.2f}"  # Two decimal places

# Update variables with formatted temperature values
indigo.variable.updateValue(987654321, temp_no_decimals)
indigo.variable.updateValue(987654322, temp_one_decimal)
indigo.variable.updateValue(987654323, temp_two_decimals)


In this code:
- `:.0f`, `:.1f`, and `:.2f` control the number of decimal places in the formatted string. The `f` indicates that the value should be formatted as a float.
- `temp_no_decimals`, `temp_one_decimal`, and `temp_two_decimals` store the formatted temperature values with different decimal places.
- `indigo.variable.updateValue()` is called for each formatted temperature value to update the respective variables in Indigo.

Regarding your question about `getValue(float, default=xx)`, if you're not sure whether the value returned by `getValue()` will always be a float and you want to provide a default value in case it's not, you can indeed use the `default` parameter. However, it's optional, and if you don't provide it, the default value will be `None`. So, it depends on whether you expect `getValue()` to return non-float values and how you want to handle those cases. If you're confident that it will always return a float, you can omit the `default` parameter.


ChatGPT really is helpful for Python novices, but you need to check the output and if it does give code that errors then just paste the Indigo log error lines and it will reformat.

CliveS

Indigo 2023.2.0 : macOS Ventura 13.6.3 : Mac Mini M2 : 8‑core CPU and 10‑core GPU : 8 GB : 256GB SSD
----------------------------------------------------------------------------------
The best way to get the right answer on the Internet is not to ask a question, it's to post the wrong answer

Posted on
Sat Apr 20, 2024 3:48 pm
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Re: Temperature Conversion

CliveS wrote:
I copied your question verbatim into my chatGPT and got the following reply.

ChatGPT really is helpful for Python novices, but you need to check the output and if it does give code that errors then just paste the Indigo log error lines and it will reformat.


PERFECT! Exactly what I was looking for and lots of choices to modify. Thanks CliveS (Its a little creepy that AI can do that)

Posted on
Tue Apr 23, 2024 12:29 pm
DaveL17 offline
User avatar
Posts: 6769
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Temperature Conversion

I meant to post this earlier and then forgot. The formatting statements above are called "Formatted String Literals" (or F-Strings for short) and they're really powerful. In addition to the math operations shown, you can also do things like logical operations. For example,

Code: Select all
print(f'{x} mph. {"" if x < 100 else "Holy Crap!"}')

x = 99
>>>  99 mph.

x = 100
>>> 100 mph. Holy Crap!
These logical operations are especially useful when you want to call attention to something that's abnormal. There are a lot of good examples in the Python docs.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest