Skip to content

Commit

Permalink
Add temperature conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergi0Martin committed Jun 13, 2024
1 parent 3bcb071 commit bac0ab1
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 12 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,28 @@
**MeasurementsTool** are simple C# Enum types with cool extension methods for measurement conversions.
### Features
#### Available conversion types:
- LengthUnit:
- Length:
> **International System**:
KM, HM, DAM, M, DM, CM, MM
> **Non International System**:
YD, FT, IN


- WeightUnit:
- Weight:
> **International System**:
T, Q, KG, HG, DAG, G, DG, CG, MG, MCG

> **Non International System**:
LongTon, ShortTon, LB, OZ

- Capacity:
> **International System**:
YL, ZL, EL, PL, TL, GL, ML, kL, hL, daL, L, dL, cL, mL, uL, nL, pL, fL, aL, zL, yL

- Temperature:
> **International System**:
Celsius, Fahrenheit, Kelvin

### Usage
```csharp
WeightUnitType.<fromWeightUnit>.ConvertTo(<desiredWeightUnit>, <weightValue>)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/components/LatestReleases/LatestReleases.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Release from "./Release.astro";
<section class="mb-10 md:mb-20">
<h1 class="dark:text-maize text-xl font-bold">Latest Releases</h1>
<div class="flex flex-row justify-center text-center">
<Release version="1.2.0" />
<Release version="1.1.0" />
<Release version="1.0.15" />
<Release version="1.0.14" />
</div>
</section>
<!-- <script>
Expand Down
4 changes: 2 additions & 2 deletions src/MeasurementsTool/MeasurementsTool.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<PackageTags>measurement;conversion;length,weight</PackageTags>
<Description>Measurement units with extension conversion methods</Description>
<Authors>Sergio Martin</Authors>
<Version>1.1.0</Version>
<Version>1.2.0</Version>
<RepositoryUrl>https://github.com/Sergi0Martin/MeasurementsTool</RepositoryUrl>
<Title>MeasurementsTool</Title>
<PackageProjectUrl>https://sergi0martin.github.io/MeasurementsTool</PackageProjectUrl>
Expand Down
58 changes: 58 additions & 0 deletions src/MeasurementsTool/UnitTypes/TemperatureType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace MeasurementsTool.UnitTypes;

public enum TemperatureType
{
// International System
Kelvin,
Celsius,
Fahrenheit,
}

public static class TemperatureConverter
{
public static double ConvertTo(this TemperatureType from, TemperatureType to, double value)
{
if (from == to)
{
return value;
}

if (from == TemperatureType.Kelvin)
{
if (to == TemperatureType.Celsius)
{
return value - 273.15;
}
if (to == TemperatureType.Fahrenheit)
{
return value * 9 / 5 - 459.67;
}
}

if (from == TemperatureType.Celsius)
{
if (to == TemperatureType.Kelvin)
{
return value + 273.15;
}
if (to == TemperatureType.Fahrenheit)
{
return value * 9 / 5 + 32;
}
}

if (from == TemperatureType.Fahrenheit)
{
if (to == TemperatureType.Kelvin)
{
return (value + 459.67) * 5 / 9;
}
if (to == TemperatureType.Celsius)
{
return (value - 32) * 5 / 9;
}
}

throw new NotImplementedException("Conversion not implemented");
}
}
27 changes: 27 additions & 0 deletions test/UnitTest/TemperatureTypeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace MeasurementsTool.UnitTests;

public sealed class TemperatureTypeTest
{
[SetUp]
public void Setup() { }

[Theory]
[TestCase(TemperatureType.Celsius, TemperatureType.Fahrenheit, 50.5, 122.9)]
[TestCase(TemperatureType.Celsius, TemperatureType.Kelvin, 50.5, 323.65)]
[TestCase(TemperatureType.Fahrenheit, TemperatureType.Celsius, 50.5, 10.277777777777779)]
[TestCase(TemperatureType.Fahrenheit, TemperatureType.Kelvin, 50.5, 283.42777777777775)]
[TestCase(TemperatureType.Kelvin, TemperatureType.Celsius, 50.5, -222.64999999999998)]
[TestCase(TemperatureType.Kelvin, TemperatureType.Fahrenheit, 50.5, -368.77)]
public void ConvertTo_International_System_Works(TemperatureType from, TemperatureType to, double value, double expectedResult)
{
// Act
var conversion = from.ConvertTo(
to: to,
value: value);

// Assert
conversion
.Should()
.Be(expectedResult);
}
}
6 changes: 0 additions & 6 deletions test/UnitTest/WeightUnitTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ public class WeightUnitTypeTest
[SetUp]
public void Setup() { }

[Test]
public void Test1()
{
Assert.Pass();
}

[Theory]
[TestCase(WeightUnitType.KG, WeightUnitType.G, 50.5, 50500)]
[TestCase(WeightUnitType.G, WeightUnitType.T, 50.5, 0.0000505)]
Expand Down

0 comments on commit bac0ab1

Please sign in to comment.