SharpConvert 1.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package SharpConvert --version 1.2.1
NuGet\Install-Package SharpConvert -Version 1.2.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SharpConvert" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SharpConvert --version 1.2.1
#r "nuget: SharpConvert, 1.2.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SharpConvert as a Cake Addin
#addin nuget:?package=SharpConvert&version=1.2.1

// Install SharpConvert as a Cake Tool
#tool nuget:?package=SharpConvert&version=1.2.1

Codacy Badge Build status

Sharp Convert

Purpose - disclaimer

The purpose of this library is to handle conversions between units of measurement in an object oriented manner. Although I recognize the library is not complete, the intention is neither to describe every know unit, nor describe all possible physics equations that involve units.

For some background for the inspiration, you can have a look at this relevant question in stackoverflow. Basically the goal was to have something similar to F# units of measure which unfortunately is not even remotely possible. There is no way to get dimensional analysis in compile time and creating derivative units (like the example right below) is simply impossible.

[<Measure>] type N = kg m/sec^2

Regardless if you are still interested and find some value into knowing what is the number you see in your code then read on.

Introduction

I realized the importance of an object oriented units conversion library while I was developing DARSSY. There I had to deal with nautical miles, feet, knots and feet per minute all at the same time. That was easy at first, but when the project scaled up, I was searching for bugs the root of which was units conversion. For example, when calculating ILS glide paths I had to calculate the tangent of the glide triangle, and that was altitude in feet divided by distance in nautical miles. Before I had to do:

double distance = ...;
double tan = aircraft.Altitude * 0.3048 / (distance * 1852);
double glideSlopeDegrees = Math.Atan(tan) / Math.PI * 180;

Now I can do:

LengthUnit distance = ...;
double tan = aircraft.Altitude / distance;
Degrees = glideSlope = Math.Atan(tan).Radians().To<Degrees>();

and the library takes care of the rest.

Characteristics

Fluent interface

Sharp Units approaches object creation by utilizing a fluent interface. For example:

Meters distance = 150.Meters();

And for conversions you can use:

Feet ft = distance.To<Feet>();

Type safety

SharpConvert is type safe. Each unit type is a class by itself. Each time you know that you are handling distance or angle and not some cryptic double or float value. You won't end up adding up acceleration to temperature by mistake.

Performance

Type safety comes at a cost. New object allocation is expensive and this is what you do each time you create a unit. In fact this

30.Meters() < 2.Meters();

is (approximately) twice as slow as checking for

30.Meters().UnitValue < 2;

Problem is that 2 can be anything and you won't always know what's on the left side of the operator.

Accuracy

As it's already known, floating point arithmetic has rounding errors. For example 0.1 is not stored as exactly 0.1. To deal with the problem, Sharp Convert has an error margin of 10<sup>-15</sup> when performing equality operations.

Parsing

Parsing is based on the unit symbol and is case sensitive. For example 12NM will result in 12 nautical miles while 12nm will be 12 nanometers. Clearly, several orders of magnitude different. You don't have to worry for now as I don't have any nanometers in the library. But future can be unpredictable. Space is allowed between the number and the unit symbol, but it's not advised as this might change in the future.

Available units

Length
  • Meters
  • Feet
  • Kilometers
  • NauticalMiles
Time
  • Second
  • Minute
  • Hour
Speed
  • MetersPerSecond
  • FeetPerSecond
  • FeetPerMinute
  • Knots
Acceleration
  • MetersPerSecondSquared
  • FeetPerSecondSquared
  • FeetPerMinutePerSecond
  • KnotsPerSecond
Angle
  • Radians
  • Degrees
Angular velocity
  • RadiansPerSecond
  • DegreesPerSecond
Mass
  • Kilograms
Product Compatible and additional computed target framework versions.
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on SharpConvert:

Package Downloads
SharpConvert.JsonNet

JSON.net additions for SharpConvert.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.3.0 169 6/21/2023
1.2.2 158 4/11/2023
1.2.1 434 5/30/2022
1.2.0 400 5/29/2022
1.1.0.4 459 7/7/2021
1.1.0.3 319 5/17/2021
1.1.0.2 300 5/11/2021
1.0.40 318 2/13/2021
1.0.39 454 11/29/2020
1.0.38 420 11/20/2020
1.0.37 440 10/14/2020
1.0.36 423 10/4/2020
1.0.35 372 10/3/2020
1.0.34 399 9/30/2020
1.0.33 429 6/17/2020
1.0.32 476 6/16/2020
1.0.31 453 6/13/2020
1.0.29 434 6/4/2020
1.0.28 498 6/2/2020
1.0.27 445 5/17/2020
1.0.1 450 5/17/2020
1.0.0 85 1/14/2024