Skip to content

Instantly share code, notes, and snippets.

@jgphilpott
Last active May 16, 2023 19:05
Show Gist options
  • Save jgphilpott/8260eeb1816dd4d786268f0ac5085d55 to your computer and use it in GitHub Desktop.
Save jgphilpott/8260eeb1816dd4d786268f0ac5085d55 to your computer and use it in GitHub Desktop.
A collection of functions for converting between different units of temperature.
### Temperature Conversions ###
convertTemperature =
celsius: {}
fahrenheit: {}
kelvin: {}
### Celsius Conversions ###
convertTemperature.celsius.celsius = c$c = (c) -> c # Celsius to Celsius
convertTemperature.celsius.fahrenheit = c$f = (c) -> c * (9 / 5) + 32 # Celsius to Fahrenheit
convertTemperature.celsius.kelvin = c$k = (c) -> c + 273.15 # Celsius to Kelvin
### Fahrenheit Conversions ###
convertTemperature.fahrenheit.celsius = f$c = (f) -> (f - 32) * (5 / 9) # Fahrenheit to Celsius
convertTemperature.fahrenheit.fahrenheit = f$f = (f) -> f # Fahrenheit to Fahrenheit
convertTemperature.fahrenheit.kelvin = f$k = (f) -> (f - 32) * (5 / 9) + 273.15 # Fahrenheit to Kelvin
### Kelvin Conversions ###
convertTemperature.kelvin.celsius = k$c = (k) -> k - 273.15 # Kelvin to Celsius
convertTemperature.kelvin.fahrenheit = k$f = (k) -> (k - 273.15) * (9 / 5) + 32 # Kelvin to Fahrenheit
convertTemperature.kelvin.kelvin = k$k = (k) -> k # Kelvin to Kelvin
/* Temperature Conversions */
var c$c, c$f, c$k, convertTemperature, f$c, f$f, f$k, k$c, k$f, k$k;
convertTemperature = {
celsius: {},
fahrenheit: {},
kelvin: {}
};
/* Celsius Conversions */
convertTemperature.celsius.celsius = c$c = function(c) {
return c; // Celsius to Celsius
};
convertTemperature.celsius.fahrenheit = c$f = function(c) {
return c * (9 / 5) + 32; // Celsius to Fahrenheit
};
convertTemperature.celsius.kelvin = c$k = function(c) {
return c + 273.15; // Celsius to Kelvin
};
/* Fahrenheit Conversions */
convertTemperature.fahrenheit.celsius = f$c = function(f) {
return (f - 32) * (5 / 9); // Fahrenheit to Celsius
};
convertTemperature.fahrenheit.fahrenheit = f$f = function(f) {
return f; // Fahrenheit to Fahrenheit
};
convertTemperature.fahrenheit.kelvin = f$k = function(f) {
return (f - 32) * (5 / 9) + 273.15; // Fahrenheit to Kelvin
};
/* Kelvin Conversions */
convertTemperature.kelvin.celsius = k$c = function(k) {
return k - 273.15; // Kelvin to Celsius
};
convertTemperature.kelvin.fahrenheit = k$f = function(k) {
return (k - 273.15) * (9 / 5) + 32; // Kelvin to Fahrenheit
};
convertTemperature.kelvin.kelvin = k$k = function(k) {
return k; // Kelvin to Kelvin
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment