需要在c#中进行double类型的枚举
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/655038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Need to make an enumeration of type double in c#
提问by
How can I create an enum of type double?
如何创建 double 类型的枚举?
Is it possible or do I have to create some kind of collection and hash?
是否有可能或者我必须创建某种集合和哈希?
回答by tvanfosson
回答by user76035
You can't make it an enumeration.
你不能让它成为一个枚举。
http://msdn.microsoft.com/en-us/library/y94acxy2.aspx
http://msdn.microsoft.com/en-us/library/y94acxy2.aspx
One of possibilities:
一种可能性:
public static class MyPseudoEnum
{
public static readonly double FirstVal = 0.3;
public static readonly double SecondVal = 0.5;
}
回答by amaca
I think byte, sbyte, short, ushort, int, uint, long, or ulong are your only options.
我认为 byte、sbyte、short、ushort、int、uint、long 或 ulong 是您唯一的选择。
Why, as a matter of interest? Maybe we can work round it.
为什么,作为一个利益问题?也许我们可以解决它。
回答by Kelsey
It is not possible to use double for an enum. See the following MSDN article for an explination of how enums work:
不可能对枚举使用 double 。有关枚举如何工作的说明,请参阅以下 MSDN 文章:
Here is a quote from the article that answers your question:
以下是回答您问题的文章的引述:
Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int.
每个枚举类型都有一个底层类型,它可以是除 char 之外的任何整数类型。枚举元素的默认基础类型是 int。
回答by itsmatt
No, enums are essentially integer values.
不,枚举本质上是整数值。
I don't know about your particular problem you're solving but you could do a generic dictionary.
我不知道你正在解决的特定问题,但你可以做一个通用字典。
Of course, comparison of doubles is always fun, particularly when they differ way down in the decimals places. Might be interesting if you try to compare the results of a calculation (for equality) against a known double. Just a caution, if that is what you might need to do.
当然,比较双打总是很有趣的,特别是当它们在小数位上有很大不同时。如果您尝试将计算结果(相等性)与已知双精度值进行比较,可能会很有趣。只是一个警告,如果这是你可能需要做的。
回答by Jim Mischel
I guess it depends on how much trouble you want to go to. You canconvert a double to a long, so if you converted your double values to long integers in a separate program, and output the constant values, you could then use them in your program.
我想这取决于你想解决多少麻烦。您可以将 double 转换为 long,因此如果您在单独的程序中将 double 值转换为 long 整数,并输出常量值,则可以在程序中使用它们。
I would strongly discourage this, by the way, but sometimes you gotta do what ya gotta do.
顺便说一下,我强烈反对这样做,但有时你必须做你必须做的事情。
First, write a program to convert your doubles to longs:
首先,编写一个程序将双精度转换为长整数:
Console.WriteLine("private const long x1 = 0x{0:X16}L;",
BitConverter.DoubleToInt64Bits(0.5));
Console.WriteLine("private const long x2 = 0x{0:X16}L;",
BitConverter.DoubleToInt64Bits(3.14));
Now, add that output to your program and create the enum:
现在,将该输出添加到您的程序并创建枚举:
private const long x1 = 0x3FE0000000000000L;
private const long x2 = 0x40091EB851EB851FL;
enum MyEnum : long
{
val1 = x1,
val2 = x2
}
When you want to test a double value against the enum, you'll have to call BitConverter.DoubleToInt64Bits()
, and cast the result to your enum type. To convert the other way, call BitConverter.Int64BitsToDouble()
.
当您想针对枚举测试双精度值时,您必须调用BitConverter.DoubleToInt64Bits()
,并将结果转换为您的枚举类型。要以另一种方式转换,请调用BitConverter.Int64BitsToDouble()
。
回答by Stijn Bollen
Why do you need to use an enum of type double?
为什么需要使用 double 类型的枚举?
If you need sub values, you could just multiply the double values with a constant operator to turn them all into integers (and maintain the same ratio) and then use that same operator to turn your values back to doubles afterwards... If you multiply by the dozens it would just be like temporarily moving the decimal separator to the left. Simple but effective.
如果您需要子值,您可以将双精度值与常量运算符相乘以将它们全部转换为整数(并保持相同的比率),然后使用相同的运算符将您的值转回双精度值......如果您乘以通过几十个,就像暂时将小数点分隔符向左移动一样。简单但有效。