Sign in to follow this  
Followers 0

_Temperature() - Convert various temperatures to different units for example Celsius to Fahrenheit.


14 posts in this topic

Posted (edited)

Here is a UDF I came up with to convert various temperatures to different units for example Celsius to Fahrenheit.

Any suggestions or questions then please post below. Thanks.

Note: I will probably add more units over time depending on whether or not I see it is worth it.

UDF:

#include-once

; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
; #INDEX# =======================================================================================================================
; Title .........: Temperature
; AutoIt Version : v3.2.2.0 or higher
; Language ......: English
; Description ...: Convert various temperatures to different units for example Celsius to Fahrenheit.
; Note ..........:
; Author(s) .....: guinness, with Reaumur functions kindly provided by Andreik.
; Remarks .......: Wikipedia was used for the conversion formulas: https://en.wikipedia.org/wiki/Temperature_conversion_formulas
; ===============================================================================================================================

; #INCLUDES# =========================================================================================================
; None

; #GLOBAL VARIABLES# =================================================================================================
; None

; #CURRENT# =====================================================================================================================
; _CelsiusToFahrenheit: Convert a temperature in Celsius to Fahrenheit.
; _CelsiusToKelvin: Convert a temperature in Celsius to Kelvin.
; _CelsiusToRankine: Convert a temperature in Celsius to Rankine.
; _CelsiusToReaumur: Convert a temperature in Celsius to Reaumur.

; _FahrenheitToCelsius: Convert a temperature in Fahrenheit to Celsius.
; _FahrenheitToKelvin: Convert a temperature in Fahrenheit to Kelvin.
; _FahrenheitToRankine: Convert a temperature in Fahrenheit to Rankine.
; _FahrenheitToReaumur: Convert a temperature in Fahrenheit to Reaumur.

; _KelvinToCelsius: Convert a temperature in Kelvin to Celsius.
; _KelvinToFahrenheit: Convert a temperature in Kelvin to Fahrenheit.
; _KelvinToRankine: Convert a temperature in Kelvin to Rankine.
; _KelvinToReaumur: Convert a temperature in Kelvin to Reaumur.

; _RankineToCelsius: Convert a temperature in Rankine to Celsius.
; _RankineToFahrenheit: Convert a temperature in Rankine to Fahrenheit.
; _RankineToKelvin: Convert a temperature in Rankine to Kelvin.
; _RankineToReaumur: Convert a temperature in Rankine to Reaumur.

; _ReaumurToCelsius: Convert a temperature in Reaumur to Celsius.
; _ReaumurToFahrenheit: Convert a temperature in Reaumur to Fahrenheit.
; _ReaumurToKelvin: Convert a temperature in Reaumur to Kelvin.
; _ReaumurToRankine: Convert a temperature in Reaumur to Rankine.
; ===============================================================================================================================

; #INTERNAL_USE_ONLY#============================================================================================================
; None
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name ..........: _CelsiusToFahrenheit
; Description ...: Convert a temperature in Celsius to Fahrenheit.
; Syntax ........: _CelsiusToFahrenheit($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Celsius
; Example .......: Yes
; ===============================================================================================================================
Func _CelsiusToFahrenheit($iTemperature)
	Return $iTemperature * (9 / 5) + 32
EndFunc   ;==>_CelsiusToFahrenheit

; #FUNCTION# ====================================================================================================================
; Name ..........: _CelsiusToKelvin
; Description ...: Convert a temperature in Celsius to Kelvin.
; Syntax ........: _CelsiusToKelvin($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Celsius
; Example .......: Yes
; ===============================================================================================================================
Func _CelsiusToKelvin($iTemperature)
	Return $iTemperature + 273.15
EndFunc   ;==>_CelsiusToKelvin

; #FUNCTION# ====================================================================================================================
; Name ..........: _CelsiusToRankine
; Description ...: Convert a temperature in Celsius to Rankine.
; Syntax ........: _CelsiusToRankine($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Celsius
; Example .......: Yes
; ===============================================================================================================================
Func _CelsiusToRankine($iTemperature)
	Return ($iTemperature + 273.15) * (9 / 5)
EndFunc   ;==>_CelsiusToRankine

; #FUNCTION# ====================================================================================================================
; Name ..........: _CelsiusToReaumur
; Description ...: Convert a temperature in Celsius to Reaumur.
; Syntax ........: _CelsiusToReaumur($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Celsius
; Example .......: Yes
; ===============================================================================================================================
Func _CelsiusToReaumur($iTemperature)
	Return $iTemperature * (4 / 5)
EndFunc   ;==>_CelsiusToReaumur

; #FUNCTION# ====================================================================================================================
; Name ..........: _FahrenheitToCelsius
; Description ...: Convert a temperature in Fahrenheit to Celsius.
; Syntax ........: _FahrenheitToCelsius($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Fahrenheit
; Example .......: Yes
; ===============================================================================================================================
Func _FahrenheitToCelsius($iTemperature)
	Return ($iTemperature - 32) * (5 / 9)
EndFunc   ;==>_FahrenheitToCelsius

; #FUNCTION# ====================================================================================================================
; Name ..........: _FahrenheitToKelvin
; Description ...: Convert a temperature in Fahrenheit to Kelvin.
; Syntax ........: _FahrenheitToKelvin($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Fahrenheit
; Example .......: Yes
; ===============================================================================================================================
Func _FahrenheitToKelvin($iTemperature)
	Return ($iTemperature + 459.67) * (5 / 9)
EndFunc   ;==>_FahrenheitToKelvin

; #FUNCTION# ====================================================================================================================
; Name ..........: _FahrenheitToRankine
; Description ...: Convert a temperature in Fahrenheit to Rankine.
; Syntax ........: _FahrenheitToRankine($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Fahrenheit
; Example .......: Yes
; ===============================================================================================================================
Func _FahrenheitToRankine($iTemperature)
	Return $iTemperature + 459.67
EndFunc   ;==>_FahrenheitToRankine

; #FUNCTION# ====================================================================================================================
; Name ..........: _FahrenheitToReaumur
; Description ...: Convert a temperature in Fahrenheit to Reaumur.
; Syntax ........: _FahrenheitToReaumur($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Fahrenheit
; Example .......: Yes
; ===============================================================================================================================
Func _FahrenheitToReaumur($iTemperature)
	Return ($iTemperature - 32) * (4 / 9)
EndFunc   ;==>_FahrenheitToReaumur

; #FUNCTION# ====================================================================================================================
; Name ..........: _KelvinToCelsius
; Description ...: Convert a temperature in Kelvin to Celsius.
; Syntax ........: _KelvinToCelsius($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Kelvin
; Example .......: Yes
; ===============================================================================================================================
Func _KelvinToCelsius($iTemperature)
	Return $iTemperature - 273.15
EndFunc   ;==>_KelvinToCelsius

; #FUNCTION# ====================================================================================================================
; Name ..........: _KelvinToFahrenheit
; Description ...: Convert a temperature in Kelvin to Fahrenheit.
; Syntax ........: _KelvinToFahrenheit($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Kelvin
; Example .......: Yes
; ===============================================================================================================================
Func _KelvinToFahrenheit($iTemperature)
	Return ($iTemperature * (9 / 5)) - 459.67
EndFunc   ;==>_KelvinToFahrenheit

; #FUNCTION# ====================================================================================================================
; Name ..........: _KelvinToRankine
; Description ...: Convert a temperature in Kelvin to Rankine.
; Syntax ........: _KelvinToRankine($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Kelvin
; Example .......: Yes
; ===============================================================================================================================
Func _KelvinToRankine($iTemperature)
	Return $iTemperature * (9 / 5)
EndFunc   ;==>_KelvinToRankine

; #FUNCTION# ====================================================================================================================
; Name ..........: _KelvinToReaumur
; Description ...: Convert a temperature in Kelvin to Reaumur.
; Syntax ........: _KelvinToReaumur($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Kelvin
; Example .......: Yes
; ===============================================================================================================================
Func _KelvinToReaumur($iTemperature)
	Return ($iTemperature - 273.15) * (4 / 5)
EndFunc   ;==>_KelvinToReaumur

; #FUNCTION# ====================================================================================================================
; Name ..........: _RankineToCelsius
; Description ...: Convert a temperature in Rankine to Celsius.
; Syntax ........: _RankineToCelsius($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Rankine
; Example .......: Yes
; ===============================================================================================================================
Func _RankineToCelsius($iTemperature)
	Return ($iTemperature - 491.67) * (5 / 9)
EndFunc   ;==>_RankineToCelsius

; #FUNCTION# ====================================================================================================================
; Name ..........: _RankineToFahrenheit
; Description ...: Convert a temperature in Rankine to Fahrenheit.
; Syntax ........: _RankineToFahrenheit($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Rankine
; Example .......: Yes
; ===============================================================================================================================
Func _RankineToFahrenheit($iTemperature)
	Return $iTemperature - 459.67
EndFunc   ;==>_RankineToFahrenheit

; #FUNCTION# ====================================================================================================================
; Name ..........: _RankineToKelvin
; Description ...: Convert a temperature in Rankine to Kelvin.
; Syntax ........: _RankineToKelvin($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Rankine
; Example .......: Yes
; ===============================================================================================================================
Func _RankineToKelvin($iTemperature)
	Return $iTemperature * (5 / 9)
EndFunc   ;==>_RankineToKelvin

; #FUNCTION# ====================================================================================================================
; Name ..........: _RankineToReaumur
; Description ...: Convert a temperature in Rankine to Reaumur.
; Syntax ........: _RankineToReaumur($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness
; Link ..........: https://en.wikipedia.org/wiki/Rankine
; Example .......: Yes
; ===============================================================================================================================
Func _RankineToReaumur($iTemperature)
	Return ($iTemperature - 491.67) * (4 / 9)
EndFunc   ;==>_RankineToReaumur

; #FUNCTION# ====================================================================================================================
; Name ..........: _ReaumurToCelsius
; Description ...: Convert a temperature in Reaumur to Celsius.
; Syntax ........: _ReaumurToCelsius($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness & Andreik
; Link ..........: https://en.wikipedia.org/wiki/Réaumur_scale
; Example .......: Yes
; ===============================================================================================================================
Func _ReaumurToCelsius($iTemperature)
	Return $iTemperature * (5 / 4)
EndFunc   ;==>_ReaumurToCelsius

; #FUNCTION# ====================================================================================================================
; Name ..........: _ReaumurToFahrenheit
; Description ...: Convert a temperature in Reaumur to Fahrenheit.
; Syntax ........: _ReaumurToFahrenheit($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness & Andreik
; Link ..........: https://en.wikipedia.org/wiki/Réaumur_scale
; Example .......: Yes
; ===============================================================================================================================
Func _ReaumurToFahrenheit($iTemperature)
	Return $iTemperature * (9 / 4) + 32
EndFunc   ;==>_ReaumurToFahrenheit

; #FUNCTION# ====================================================================================================================
; Name ..........: _ReaumurToKelvin
; Description ...: Convert a temperature in Reaumur to Kelvin.
; Syntax ........: _ReaumurToKelvin($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness & Andreik
; Link ..........: https://en.wikipedia.org/wiki/Réaumur_scale
; Example .......: Yes
; ===============================================================================================================================
Func _ReaumurToKelvin($iTemperature)
	Return $iTemperature * (5 / 4) + 273.15
EndFunc   ;==>_ReaumurToKelvin

; #FUNCTION# ====================================================================================================================
; Name ..........: _ReaumurToRankine
; Description ...: Convert a temperature in Reaumur to Rankine.
; Syntax ........: _ReaumurToRankine($iTemperature)
; Parameters ....: $iTemperature        - An integer value.
; Return values .: Converted temperature.
; Author ........: guinness & Andreik
; Link ..........: https://en.wikipedia.org/wiki/Réaumur_scale
; Example .......: Yes
; ===============================================================================================================================
Func _ReaumurToRankine($iTemperature)
	Return $iTemperature * (9 / 4) + 491.67
EndFunc   ;==>_ReaumurToRankine

Example 1:

#include 'Temperature.au3'

Example()

Func Example()
	ConsoleWrite('_CelsiusToFahrenheit: ' & _CelsiusToFahrenheit(0) & ChrW(176) & 'F' & @CRLF)
	ConsoleWrite('_CelsiusToKelvin: ' & _CelsiusToKelvin(0) & 'K' & @CRLF)
	ConsoleWrite('_CelsiusToRankine: ' & _CelsiusToRankine(0) & ChrW(176) & 'R' & @CRLF)
	ConsoleWrite('_CelsiusToReaumur: ' & _CelsiusToReaumur(0) & ChrW(176) & 'Re' & @CRLF & @CRLF)

	ConsoleWrite('_FahrenheitToCelsius: ' & _FahrenheitToCelsius(32) & ChrW(176) & 'C' & @CRLF)
	ConsoleWrite('_FahrenheitToKelvin: ' & _FahrenheitToKelvin(32) & 'K' & @CRLF)
	ConsoleWrite('_FahrenheitToRankine: ' & _FahrenheitToRankine(32) & ChrW(176) & 'R' & @CRLF)
	ConsoleWrite('_FahrenheitToReaumur: ' & _FahrenheitToReaumur(32) & ChrW(176) & 'Re' & @CRLF & @CRLF)

	ConsoleWrite('_KelvinToCelsius: ' & _KelvinToCelsius(273.15) & ChrW(176) & 'C' & @CRLF)
	ConsoleWrite('_KelvinToFahrenheit: ' & _KelvinToFahrenheit(273.15) & ChrW(176) & 'F' & @CRLF)
	ConsoleWrite('_KelvinToRankine: ' & _KelvinToRankine(273.15) & ChrW(176) & 'R' & @CRLF)
	ConsoleWrite('_KelvinToReaumur: ' & _KelvinToReaumur(273.15) & ChrW(176) & 'Re' & @CRLF & @CRLF)

	ConsoleWrite('_RankineToCelsius: ' & ': ' & _RankineToCelsius(491.67) & ChrW(176) & 'C' & @CRLF)
	ConsoleWrite('_RankineToFahrenheit: ' & _RankineToFahrenheit(491.67) & ChrW(176) & 'F' & @CRLF)
	ConsoleWrite('_RankineToKelvin: ' & _RankineToKelvin(491.67) & 'K' & @CRLF)
	ConsoleWrite('_RankineToReaumur: ' & _RankineToReaumur(491.67) & 'Re' & @CRLF & @CRLF)

	ConsoleWrite('_ReaumurToCelsius: ' & ': ' & _ReaumurToCelsius(0) & ChrW(176) & 'C' & @CRLF)
	ConsoleWrite('_ReaumurToFahrenheit: ' & _ReaumurToFahrenheit(0) & ChrW(176) & 'F' & @CRLF)
	ConsoleWrite('_ReaumurToKelvin: ' & _ReaumurToKelvin(0) & 'K' & @CRLF)
	ConsoleWrite('_ReaumurToRankine: ' & _ReaumurToRankine(0) & 'R' & @CRLF)
EndFunc   ;==>Example

All of the above has been included in a ZIP file. Temperature.zip

Edited by guinness

Share this post


Link to post
Share on other sites



Posted

Why not make the UDF a general conversion UDF, add things other than Temperature, like KHz to GHz, Centimenters to inches, etc.

Share this post


Link to post
Share on other sites

Posted (edited)

Why not make the UDF a general conversion UDF, add things other than Temperature, like KHz to GHz, Centimenters to inches, etc.

Ease really, but I suppose units of length would be another good UDF e.g. feet to centimetres. Thanks. Edited by guinness

Share this post


Link to post
Share on other sites

Posted

And Mph To Kmh but i suppose where do you stop lol

Share this post


Link to post
Share on other sites

Posted

Yeh, this is why separate UDFs would be easier to maintain and for the end user to follow what the UDF includes/does.

Share this post


Link to post
Share on other sites

Posted

Or just write the smaller ones as wrappers i guess

Good work either way

Share this post


Link to post
Share on other sites

Posted (edited)

Or just write the smaller ones as wrappers i guess

Good work either way

Or that too, but I don't want to bog down the wrappers section with people asking questions for additions etc. Edited by guinness

Share this post


Link to post
Share on other sites

Posted (edited)

Maybe you can add also functions for conversion using Reaumur scale. Sometimes is used in high school.

Func ReaumurToCelsius($iTemperature)
	Return $iTemperature * (5 / 4)
EndFunc

Func ReaumurToFahrenheit($iTemperature)
	Return $iTemperature * (9 / 4) + 32
EndFunc

Func ReaumurToKelvin($iTemperature)
	Return $iTemperature * (5 / 4) + 273.15
EndFunc

Func ReaumurToRankine($iTemperature)
	Return $iTemperature * (9 / 4) + 491.67
EndFunc

Func CelsiusToReaumur($iTemperature)
	Return $iTemperature * (4 / 5)
EndFunc

Func FahrenheitToReaumur($iTemperature)
	Return ($iTemperature - 32) * (4 / 9)
EndFunc

Func KelvinToReaumur($iTemperature)
	Return ($iTemperature - 273.15) * (4 / 5)
EndFunc

Func RankineToReaumur($iTemperature)
	Return ($iTemperature - 491.67) * (4 / 9)
EndFunc
Edited by Andreik

Share this post


Link to post
Share on other sites

Posted

Thanks Andreik, I'll add it to the UDF. I take it you got those conversion formulas from wikipedia too?

Share this post


Link to post
Share on other sites

Posted

Sure, they are not from my mind. :oops:

Share this post


Link to post
Share on other sites

Posted

You never know. I will upload tomorrow.

Share this post


Link to post
Share on other sites

Posted

I've added the Reaumur functions, thanks Andreik. Please see original post.

Share this post


Link to post
Share on other sites

Posted (edited)

What do you think about making it a single function with 3 parameters:

$fResult = TempConv($sFrom, $sTo, $fValue)

You'd call TempConv("F","C",212) or TempConv("C", "K", 100)

It would make it easier to use, and maintain, rather than keeping track of all the function names.

The Source code would shrink drastically.

Edit: Maybe "Ra" and "Re" for Rankine and Reaumer?

I was bored when I got to work this morning :oops: Having a fetish for tables, I like this approach:

MsgBox(0,"",_Tempconv("c","f",72))
MsgBox(0,"",_Tempconv("F","C",200))
MsgBox(0,"",_Tempconv("C","Ra",100))
MsgBox(0,"",_Tempconv("x","y",99))

Func _TempConv($sFrom, $sTo, $fVal)
	Local $sKey = $sFrom & $sTo, $aConversions[21][2] = [[20, ""], _
	   ["CF", "$fVal * (9 / 5) + 32"], _	; Celsius to Fahrenheit
	   ["CK", "$fVal + 273.15"], _	 ; Celsius to Kelvin
	   ["CRa", "($fVal + 273.15) * (9 / 5)"], _ ; Celsius to Rankine
	   ["CRe", "$fVal  * (4 / 5)"], _	; Celsius to Reaumur
	   ["FC", "($fVal - 32) * (5 / 9)"], _   ; Fahrenheit to Celsius
	   ["FK", "($fVal + 459.67) * (5 / 9)"], _  ; Fahrenheit to Kelvin
	   ["FRa", "$fVal + 459.67"], _	; Fahrenheit to Rankine
	   ["FRe", "($fVal - 32) * (4 / 9)"], _  ; Fahrenheit to Reaumur
	   ["KC", "$fVal - 273.15"], _	 ; Kelvin to Celsius
	   ["KF", "($fVal * (9 / 5)) - 459.67"], _  ; Kelvin to Fahrenheit
	   ["KRa", "$fVal * (9 / 5)"], _	; Kelvin to Rankine
	   ["KRe", "($fVal - 273.15) * (4 / 5)"], _ ; Kelvin to Reaumur
	   ["RaC", "$fVal - 491.67) * (5 / 9)"], _  ; Rankine to Celcius
	   ["RaF", "$fVal - 459.67"], _	; Rankine to Fahrenheit
	   ["RaK", "$fVal * (5 / 9)"], _	; Rankine to Kelvin
	   ["RaRe", "($fVal - 491.67) * (4 / 9)"], _ ; Rankine to Reaumur
	   ["ReC", "$fVal * (5 / 4)"], _	; Reaumur to Celcius
	   ["ReF", "$fVal * (9 / 4) + 32"], _   ; Reaumur to Fahrenheit
	   ["ReK", "$fVal * (5 / 4) + 273.15"], _  ; Reaumur to Kelvin
	   ["ReRa", "$fVal * (9 / 4) + 491.67"]]  ; Reaumur to Rankine

	For $x = 1 to $aConversions[0][0]
		If $sKey = $aConversions[$x][0] Then Return Execute($aConversions[$x][1])
	Next
	Return SetError(1,1,"Error")
Endfunc

Edited by Spiff59

Share this post


Link to post
Share on other sites

Posted

It's an idea actually, because when I created this I was only using Celsius & Fahrenheit so you're correct I need to look at optimising this UDF.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0