I have a situation where I need to format an integer differently depending on whether or not it's zero. Using custom numeric format strings, this can be achieved with the semicolon as a separator. Consider the following:
// this works fine but the output is decimal
string format = "{0:0000;-0000;''}";
Console.WriteLine(format, 10); // outputs "0010"
Console.WriteLine(format, -10); // outputs "-0010"
Console.WriteLine(format, 0); // outputs ""
However, the format that I want to use is hex. What I would like the output to be is more like:
// this doesn't work
string format = "{0:'0x'X8;'0x'X8;''}";
Console.WriteLine(format, 10); // desired "0x0000000A", actual "0xX8"
Console.WriteLine(format, -10); // desired "0xFFFFFFF6", actual "0xX8"
Console.WriteLine(format, 0); // desired "", actual ""
Unfortunately, when using a custom numeric format string, I am not sure how (if it's even possible) to use the hex representation of the number in the custom format string. The scenario I have doesn't permit much flexibility so doing a two-pass format isn't an option. Whatever I do needs to be represented as a String.Format style format string.
EDIT
After looking over the Mono source for NumberFormatter (the .NET implementation simply defers to internal unmanaged code) I've confirmed my suspicions. The hex format string is treated as a special case and it is only available as a standard format string and cannot be used in a custom format string. And since a three part format string can't be used with a standard format string, I'm pretty much S.O.L.
I will probably just bite the bullet and make the integer property a nullable int and use nulls where I was using zero - bleh.