The values C, E, F, G, X are among the standard format strings. I would like to add another standard string...perhaps the letter 'M' to expand my currency-formatting options. I've made a MoneyFormatInfo class that implements the necessary IFormatProvider and ICustomFormatter interfaces. It works in every scenario I can concoct, except this one...
decimal cash = 3124.728m;
//Code '392' is JAPANESE YEN, with basic French formatting.
var frenchmen = new MoneyFormatInfo("392", new CultureInfo("fr-FR"));
result = cash.ToString("m", frenchmen);
Assert.AreEqual(result, "3 124,73 JPY");
The error message I get is "FormatException was unhandled by user code".
I've reflected the BCL ToString method. I see it consults only the list of standard format strings; I don't see any hook point that would have allowed me to address this. Am I missing something?
Here are other examples that are currently working as expected...
//Code '978' is the Euro
//The custom "Money" class holds an amount and currency type which
//intentionally cannot be overridden.
Money dough = new Money(8124.348m, "978");
decimal cash = 3124.728m;
string result;
//EURO currency parameters, with basic French formatting
var french = new CultureInfo("fr-FR");
result = String.Format(french, "the money: {0:m}", dough);
Assert.AreEqual(result, "the money: 8 124,35 EUR");
//JAPANESE YEN, with basic French formatting.
var frenchmen = new MoneyFormatInfo("392", new CultureInfo("fr-FR"));
result = String.Format(frenchmen, "the cash: {0:m}", cash);
Assert.AreEqual(result, "the cash: 3 124,73 JPY");
result = dough.ToString("c", frenchmen);
Assert.AreEqual(result, "8 124,35 €");
My custom Money class has a ToString() override which performs state-changes, and also converts the 'M' format string into 'C'. In short, it works because I have control over the ToString() method. On the BCL decimal type, I do not have control over the ToString() method. I also do not want to make a custom decimal type.
string.Format("{0:m}", cash)?NumberFormatInfoin yourIFormatProvider.GetFormat()implementation? That's whatdecimal.ToString()uses