Feedback

C# - Rechner für Längeneinheiten

Veröffentlicht von am 11/21/2015
(0 Bewertungen)
Längeneinheiten-Rechner:
Angloamerikanisches Maßsystem <-> Metrisches System (SI-Einheitensystem)
    // Rechner für die gängigsten Längen im
    // Angloamerikanischen Maßsystem/Metrischen System (SI-Einheitensystem)
    public class LengthUnitConverter
    {
        // als Rechenbasis wird ein (1) Meter verwendet (Standard im Metrischen System)
        private const decimal oneMeterToNanometer = 1000000000m;
        private const decimal oneMeterToMicrometer = 1000000m;
        private const decimal oneMeterToMicron = 1000000m;
        private const decimal oneMeterToMillimeter = 1000m;
        private const decimal oneMeterToCentimeter = 100m;
        private const decimal oneMeterToDecimeter = 10m;
        private const decimal oneMeter = 1;
        private const decimal oneMeterToKillometer = 0.001m;

        private const decimal oneMeterToFurlong = 0.0049709596m;
        private const decimal oneMeterToChain = 0.049709596m;
        private const decimal oneMeterToNauticalMile = 0.000539957m;
        private const decimal oneMeterToFathom = 0.546807m;
        private const decimal oneMeterToInternationalMile = 0.000621371m;
        private const decimal oneMeterToYard = 1.09361m;
        private const decimal oneMeterToFoot = 3.28084m;
        private const decimal oneMeterToInch = 39.3701m;

        public enum Unit
        {
            Nanometer, Micrometer, Micron, Millimeter, Centimeter, Decimeter, Meter, Killometer,
            Furlong, Chain, NauticalMile, InternationalMile, Fathom, Yard, Foot, Inch
        }

        public decimal Convert(decimal value, Unit fromUnit, Unit toUnit)
        {
            return value / getUnitValue(fromUnit) * getUnitValue(toUnit);
        }

        private decimal getUnitValue(Unit aUnit)
        {
            switch (aUnit)
            {
            case Unit.Nanometer:
                {
                    return oneMeterToNanometer;
                }
                case Unit.Micrometer:
                {
                    return oneMeterToMicrometer;
                }
                case Unit.Micron:
                {
                    return oneMeterToMicron;
                }
                case Unit.Millimeter:
                {
                    return oneMeterToMillimeter;
                }
                case Unit.Centimeter:
                {
                    return oneMeterToCentimeter;
                }
                case Unit.Decimeter:
                {
                    return oneMeterToDecimeter;
                }
                case Unit.Meter:
                {
                    return oneMeter;
                }
                case Unit.Killometer:
                {
                    return oneMeterToKillometer;
                }
                case Unit.Furlong:
                {
                    return oneMeterToFurlong;
                }
                case Unit.Chain:
                {
                    return oneMeterToChain;
                }
                case Unit.NauticalMile:
                {
                    return oneMeterToNauticalMile;
                }
                case Unit.InternationalMile:
                {
                    return oneMeterToInternationalMile;
                }
                case Unit.Fathom:
                {
                    return oneMeterToFathom;
                }
                case Unit.Yard:
                {
                    return oneMeterToYard;
                }
                case Unit.Foot:
                {
                    return oneMeterToFoot;
                }
                case Unit.Inch:
                {
                    return oneMeterToInch;
                }
                default:
                {
                    return 0;
                }
            }
        }
    }
Abgelegt unter calculator, converter.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!