Giter Site home page Giter Site logo

csunits's Introduction

csunits

Simple C# framework to support Units of Measurement

Copyright 2011 © Anders Gustafsson, Cureos AB.

Made available under Eclipse Public License, version 1.0.

Prerequisites

Visual Studio 2010 Service Pack 1 or higher with Portable Library Tools (msdn.microsoft.com/en-us/library/gg597391.aspx) extension.

Introduction

Developed with Visual Studio 2010, using .NET Frameworks 3.5 and 4.0. The .NET 4.0 class library is portable and can without modifications be included in Silverlight 4 and Windows Phone (“Mango”) applications. Source code can also be compiled using Mono, at least versions 2.8 and later.

Measure<Q> and StandardMeasure<Q> are the main “work-horses” of the library. StandardMeasure<Q> is always represented in the reference unit of the associated quantity. If a different unit is specified in instantiation of StandardMeasure<Q>, the measured amount is automatically converted to the equivalent reference unit amount. On the other hand, the amount and unit used in instantiation of Measure<Q> are internally maintained.

StandardMeasure<Q> is declared as a struct and only holds one member, the amount. The main goal of this approach is to maximize calculation performance, while at the same time ensuring quantity type safety.

There are also StandardMeasureDoublet<Q1, Q2> and StandardMeasureTriplet<Q1, Q2, Q3> structures that holds two and three measures, respectively, of potentially different quantities.

Usage examples

using Cureos.Measures;
using Cureos.Measures.Quantities;
...

StandardMeasure<Mass> initialWgt = new StandardMeasure<Mass>(75.0);
StandardMeasure<Mass> gainedWgt = new StandardMeasure<Mass>(2.5, Mass.HectoGram);
StandardMeasure<Mass> newWgt = initialWgt + gainedWgt;

Measure<Mass> newWgtInGram = newWgt[Mass.Gram];
Measure<Mass> initialWgtInGram = newWgtInGram - gainedWgt;

Console.WriteLine("Initial weight: {0}", initialWgtInGram);

Measure<Length> height = new Measure<Length>(30.0, Length.CentiMeter);
StandardMeasure<Area> area = (StandardMeasure<Area>)0.02;

StandardMeasure<Volume> vol; 
ArithmeticOperations.Times(height, area, out vol);
var maxVol = new StandardMeasure<Volume>(10.0, Volume.Liter);

if (vol < maxVol)
{
  Console.WriteLine("Calculated volume is within limits, actual volume: {0}", vol[Volume.Liter]);
}

should yield the output:

Initial weight: 75000 g
Calculated volume is within limits, actual volume 6 l

Application

For pure demonstration and testing purposes, there are very simple Silverlight 4 and Windows Phone (“Mango”) unit converter applications included in the solution, SilverlightUnitConverter and WP7UnitConverter, respectively. Implementation of these applications were inspired by Andrew Cheng’s feedback and usage of csunits in his Windows Forms application.

Revision

Last updated on August 3, 2011 by Anders Gustafsson, anders[at]cureoscom, www.cureos.com.

csunits's People

Contributors

anders9ustafsson avatar generateui avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.