Click here to Skip to main content
15,893,790 members
Articles / DevOps / Unit Testing

Units of Measure Library for .NET

Rate me:
Please Sign up or sign in to vote.
4.93/5 (7 votes)
20 Jun 2012CPOL8 min read 60.4K   2K   35  
This article introduces a library for handling units of measure.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HDLibrary.UnitsOfMeasure
{
    public static class NumericExtension
    {
        #region Meter

        /// <summary>
        /// Specifies the value as meter.
        /// Note: No conversion is made!
        /// </summary>
        /// <param name="value">The value</param>
        /// <returns>The value in [m]</returns>
        [return: Unit("m")]
        public static int Meter(this int value)
        {
            return value * Units.Meter;
        }

        /// <summary>
        /// Specifies the value as meter.
        /// Note: No conversion is made!
        /// </summary>
        /// <param name="value">The value</param>
        /// <returns>The value in [m]</returns>
        [return: Unit("m")]
        public static double Meter(this double value)
        {
            return value * Units.Meter;
        }

        #endregion

        #region Second

        /// <summary>
        /// Specifies the value as second.
        /// Note: No conversion is made!
        /// </summary>
        /// <param name="value">The value</param>
        /// <returns>The value in [s]</returns>
        [return: Unit("s")]
        public static int Second(this int value)
        {
            return value * Units.Second;
        }

        /// <summary>
        /// Specifies the value as second.
        /// Note: No conversion is made!
        /// </summary>
        /// <param name="value">The value</param>
        /// <returns>The value in [s]</returns>
        [return: Unit("s")]
        public static double Second(this double value)
        {
            return value * Units.Second;
        }

        #endregion

        #region Kilogram

        /// <summary>
        /// Specifies the value as kilogram.
        /// Note: No conversion is made!
        /// </summary>
        /// <param name="value">The value</param>
        /// <returns>The value in [kg</returns>
        [return: Unit("kg")]
        public static int Kilogram(this int value)
        {
            return value * Units.Kilogram;
        }

        /// <summary>
        /// Specifies the value as kilogram.
        /// Note: No conversion is made!
        /// </summary>
        /// <param name="value">The value</param>
        /// <returns>The value in [kg]</returns>
        [return: Unit("kg")]
        public static double Kilogram(this double value)
        {
            return value * Units.Kilogram;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Germany Germany
Presently I am a student of computer science at the Karlsruhe Institute of Technology in Germany.

Comments and Discussions