Need to do an enumeration of type double in C #

How can I create an enumeration of type double?

Is this possible, or do I need to create some kind of collection and hash?

+2
source share
7 answers

You cannot do this by listing.

http://msdn.microsoft.com/en-us/library/y94acxy2.aspx

One of the possibilities:

public static class MyPseudoEnum { public static readonly double FirstVal = 0.3; public static readonly double SecondVal = 0.5; } 
+7
source

I think it depends on how many problems you want. You can convert double to long, so if you converted your double values ​​to long integers in a separate program and output constant values, you can use them in your program.

I would strongly discourage this, by the way, but sometimes you have to do what you need.

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 this output to your program and create an enumeration:

  private const long x1 = 0x3FE0000000000000L; private const long x2 = 0x40091EB851EB851FL; enum MyEnum : long { val1 = x1, val2 = x2 } 

If you want to check the double value against an enumeration, you need to call BitConverter.DoubleToInt64Bits() and pass the result to your enumeration type. To convert another method, call BitConverter.Int64BitsToDouble() .

+3
source

No, enumerations are integer values.

I do not know about your specific problem that you are solving, but you can make a general dictionary.

Of course, comparing doubles is always funny, especially when they differ in decimal places. It may be interesting if you try to compare the calculation results (for equality) with the well-known double. Just a warning if this is what you might need.

+2
source

Enum types must be integers , except char . This does not include double , so you need to create a lookup table or set of constants.

+1
source

I think your bytes, sbyte, short, ushort, int, uint, long or ulong are your options.

Why, as a matter of interest? Maybe we can get around it.

+1
source

Cannot use double to enumerate. See the following MSDN article for an explanation of how enumerations work:

MSDN Article

Here is a quote from an article that answers your question:

Each type of enumeration has a base type, which can be any integer type except char. By default, the type of enumeration elements is int.

0
source

Why do you need to use an enumeration of type double?

If you need auxiliary values, you can simply multiply double values ​​by a constant operator to turn them into integers (and maintain the same ratio), and then use the same operator to subsequently return your values ​​back to pairs ... If you multiply by tens, it just sounds like temporarily moving the decimal separator to the left. Simple but effective.

0
source

All Articles