0

I have a question here in converting an enum to a string, but I need the conversion to be filled with zero 2 digit. example

public enum System { Unknown = 0, Mirror = 3, Order = 17 } 

the output would be this "03".

with example below it works

int value; value = 3; Console.WriteLine(value.ToString("D2")); // Displays 03 

but with enum does not work

Console.WriteLine(SourceSystem.Mirror.ToString("D2")); 

and this error appears

System.FormatException Message=Format String can be only "G", "g", "X", "x", "F", "f", "D" or "d"..... 
2
  • 4
    Cast to int? int value = (int)System.Mirror; Console.WriteLine(value.ToString("D2")) Commented Jun 25, 2019 at 21:51
  • 1
    cast your enum.value to int: ((int) System.Mirror).ToString("D2") Commented Jun 25, 2019 at 21:51

1 Answer 1

3

System.Mirror is currently of type enum, so that's why you can't call ToString("D2") on it since you need to call this method on a variable of type int. So what you should do is first cast the enum to type int and then call ToString("D2") on that casted variable like so,

Console.WriteLine(((int) System.Mirror).ToString("D2")); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.