可以对双精度变量使用Enum吗? [英] Can you use Enum for Double variables?

查看:78
本文介绍了可以对双精度变量使用Enum吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于在C#中处理单位转换的类。

I have created a class for handling Unit Conversion in C#. It is not working as it should only returning strings.

这里是该类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace RestaurantManagementSystem
{
    class unitHandler
    {
        public enum Units
        {
            Grams = 1,
            KiloGrams = 0.001,
            Milligram = 1000,
            Pounds = 0.00220462,
            Ounces = 0.035274,
            Tonnes = 0.000001,

            Litres = 1,
            Millilitres = 1000,
            Cups = 4.22675,
            FluidOunces = 33.814,
            TeaSpoon = 202.884,
            TableSpoon = 67.628,
            CubicFeet = 0.0353147,
            CubicInch = 61.0237,
            CubicCentimetres = 0.0001,
            CubicMetres = 0.001

        }

        public double Convert_Value(Units from, Units to, double quantity)
        {
            double converted_quantity = 0;

            double fromValue = quantity * Convert.ToDouble(from.ToString());

            converted_quantity = fromValue * Convert.ToDouble(to.ToString());

            return converted_quantity;
        }
    }
}

我会以某种方式想要枚举类型,以包含每个单位的转换系数的双精度值,然后将其用于转换并返回转换后的数量。

I would somehow want the enum type to contain the double values of conversion factors of each unit then use them for conversion and returning the converted quantity.

推荐答案

否, 枚举的默认类型为 int long ,则不能使用小数。您可以使用结构或枚举的类intead来进行双重

No, The default type for enum is int or long and you could not use fractional numbers with it. You can use a struct or class intead of enum for double

public struct Units
{
        public const double Grams = 1;
        public const double KiloGrams = 0.001;
        public const double Milligram = 1000;
        public const double Pounds = 0.00220462;
        public const double Ounces = 0.035274;
        public const double Tonnes = 0.000001;
        // Add Remaining units / values
}

并像

double d = Units.KiloGrams;

这篇关于可以对双精度变量使用Enum吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆