tempConvert(otemp, ctype)
Last updated April 25, 2006
Version: 1 | Requires: ColdFusion MX | Library: ScienceLib
Description:
This is an almost all inclusive function to convert temperatures between common formats. I haven't included the Rankine and Reaumure scales with this edition but they will be coming soon.
The function uses two-character strings to perform the required action. For example:
CF converts Celsius to Fahrenheit
FC converts Fahrenheit to Celsius
KC converts Kelvin to Celsius
CK converts Celsius to Kelvin
FK converts Fahrenheit to Kelvin
KF converts Kelvin to Fahrenheit
so the funtcion call would look like so:
tempconvert('30','CF')
this will convert 30 degrees Celsius to the equivalent temperature Fahrenheit.
Return Values:
Returns a string.
Example:
<cfset convertThis = '30'>
<cfoutput>#tempconvert(convertThis,convertType)#</cfoutput>
Parameters:
| Name | Description | Required |
|---|---|---|
| otemp | Temperature. | Yes |
| ctype | Two character string that determines the conversion. | Yes |
Full UDF Source:
<!---
Converts temperatures from and to Celsius, Fahrenheit and Kelvin.
@param otemp Temperature. (Required)
@param ctype Two character string that determines the conversion. (Required)
@return Returns a string.
@author Jack Poe (jackpoe@yahoo.com)
@version 1, April 9, 2007
--->
<cffunction name="tempConvert" output="false" returnType="string">
<cfargument name="otemp" required="yes" type="numeric">
<cfargument name="ctype" required="yes" type="string">
<cfif arguments.ctype IS 'CF'>
<cfset convertedtTemp = (arguments.otemp*1.8)+32>
<cfset convertedtTemp = convertedtTemp & 'º F'>
<cfreturn convertedtTemp>
<cfelseif arguments.ctype IS 'FC'>
<cfset convertedtTemp = (arguments.otemp-32)*0.5555>
<cfset convertedtTemp = convertedtTemp & 'º C'>
<cfreturn convertedtTemp>
<cfelseif arguments.ctype IS 'CK'>
<cfset convertedtTemp = arguments.otemp+273.15>
<cfset convertedtTemp = convertedtTemp & 'º K'>
<cfreturn convertedtTemp>
<cfelseif arguments.ctype IS 'KC'>
<cfset convertedtTemp = arguments.otemp-273.15>
<cfset convertedtTemp = convertedtTemp & 'º C'>
<cfreturn convertedtTemp>
<cfelseif arguments.ctype IS 'FK'>
<cfset convertedtTemp = ((arguments.otemp-32)*0.5555)+273.15>
<cfset convertedtTemp = convertedtTemp & 'º K'>
<cfreturn convertedtTemp>
<cfelseif arguments.ctype IS 'KF'>
<cfset convertedtTemp = ((arguments.otemp-273.15)*1.8)+32>
<cfset convertedtTemp = convertedtTemp & 'º K'>
<cfreturn convertedtTemp>
</cfif>
</cffunction>
Search CFLib.org
Latest Additions
Tayo Akinmade added
arrayTrim
10 day(s) ago
Will Belden added
longTime
16 day(s) ago
James Sleeman added
quickSort
26 day(s) ago
Ben Forta added
GetHostAddress
29 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)