Converter.Temperature
1.1.0
Prefix Reserved
See the version list below for details.
dotnet add package Converter.Temperature --version 1.1.0
NuGet\Install-Package Converter.Temperature -Version 1.1.0
<PackageReference Include="Converter.Temperature" Version="1.1.0" />
paket add Converter.Temperature --version 1.1.0
#r "nuget: Converter.Temperature, 1.1.0"
// Install Converter.Temperature as a Cake Addin #addin nuget:?package=Converter.Temperature&version=1.1.0 // Install Converter.Temperature as a Cake Tool #tool nuget:?package=Converter.Temperature&version=1.1.0
README
Converter.Temperature This project was created to extract out the main conversion code from the Temperature Converter applications in the other repositories. I am using this as a way to show how my development skills have changed over the years, as I get to be a better developer.
Introduction
These extension methods will convert different types of temperatures to other types.
Temperature types
- Celsius
- Fahrenheit
- Kelvin
- Gas
- Rankine
Data types
- int
- float
- double
- string
Ideas
I had a few ideas on how to do the extension methods but decided on using a fluent style.
Originally, the extensions looked like this:
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.CelsiusToFahrenheit();
Then I thought that this looked a bit naf so, I went with setting up the from first, then the to. This follows the fluent pattern.
Now it is used like this:
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromCelsius().ToFahrenheit();
What to do for edge cases
I'm working on what I want the library to do when an exception could be thrown. This is when we have the edge cases of values that are too small or too large for the type being converted to.
E.g.:
From a float type with a value close to the max value of a float, converted from Celsius to Fahrenheit. Because a Fahrenheit value is greater than the celsius value, then we can't convert correctly.
Should this throw an exception or just return the original value?
Well, returning the same value would seem like all is well, even though the converted value is wrong. Thinking about this means that for me, it should throw an exception for the calling application to handle.
If anyone has any better ideas, then I would be willing to change this behaviour.
How to use
This section will go through the ways to use the extensions for the different types.
Caveat
When you use a double, and you set a value to convert that is at or just below double.MaxValue, then your conversion will fail. This is because the double.MaxValue
is so big, the conversion calculations are too small for the double to notice.
For example, if I want to convert double.MaxValue
(Celsius) to Kelvin (which adds 273 to it), I would expect the calculation to return double.Infinity
. However, it doesn't, so I had to work around that problem.
If your value, times it by 1.01 doesn't hit the infinity, then this library will return a valid value. Your application may not be able to display it.
Exceptions
The library will throw the System.ArgumentOutOfRangeException exception if the value can not be converted.
int extensions
For each of the different temperature types, you can convert to something else.
From Celsius
- To Celsius - yes you can do this, but why would you.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromCelsius().ToCelsius();
- To Fahrenheit.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromCelsius().ToFahrenheit();
- To Kelvin.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromCelsius().ToKelvin();
- To Gas.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromCelsius().ToGas();
- To Rankine.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromCelsius().ToRankine();
From Fahrenheit
- To Celsius.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromFahrenheit().ToCelsius();
- To Fahrenheit - yes you can do this, but why would you.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromFahrenheit().ToFahrenheit();
- To Kelvin.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromFahrenheit().ToKelvin();
- To Gas.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromFahrenheit().ToGas();
- To Rankine.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromFahrenheit().ToRankine();
From Kelvin
- To Celsius.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromKelvin().ToCelsius();
- To Fahrenheit.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromKelvin().ToFahrenheit();
- To Kelvin - yes you can do this, but why would you.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromKelvin().ToKelvin();
- To Gas.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromKelvin().ToGas();
- To Rankine.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromKelvin().ToRankine();
From Gas
- To Celsius.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromGas().ToCelsius();
- To Fahrenheit.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromGas().ToFahrenheit();
- To Kelvin.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromGas().ToKelvin();
- To Gas - yes you can do this, but why would you.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromGas().ToGas();
- To Rankine.
var tempToConvert = 34; // an int.
var convertedTemp = tempToConvert.FromGas().ToRankine();
float extensions
As the types all look very similar, I will only show one conversion. I'm picking on the FromCelsius ToFahrenheit method.
var tempToConvert = 34.5f; // a float.
var convertedTemp = tempToConvert.FromCelsius().ToFahrenheit();
double extensions
This time, a FromFahrenheit ToGas method.
var tempToConvert = 34.5d; // a double.
var convertedTemp = tempToConvert.FromFahrenheit().ToGas();
string extensions
This time, a FromFahrenheit ToKelvin method.
var tempToConvert = "34.5"; // a string.
var convertedTemp = tempToConvert.FromFahrenheit().ToKelvin();
More Information
For more information on the project and where it is heading, see the wiki.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- No dependencies.
-
net5.0
- No dependencies.
-
net6.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
1.1.0.3 -
* Fixed issue 8: Added Rankine scale.
1.0.3.1 -
* Fixed issue 12: Added dotnet 6.0 support.
1.0.2.1 -
* Fixed issue 6: Added dotnet 5.0 support.
1.0.1.9 -
* Fixed issue 1: Moved hot spot.