Reputation: 630
I am looking for a way to override default decimal.ToString("c") method calls. I have written my custom format provider and it works when I do something like decimal.ToString(myprovider) but is there any way to override the default provider to route decimal.ToString("c") calls through my custom provider? Please note that its not about NumberFormat thing it works and I know I can override it. Its about the actual formatter that uses number format info and plugs it in output. The final goal is to do some calculation upon values while formatting.
Thanks
Upvotes: 0
Views: 684
Reputation: 630
One way to achieve this was to write your custom provider and then change the number format provider for current thread. This will route all decimal.ToString("c")
to your custom provider and format according to your codes.
Upvotes: 1
Reputation: 769
I can suggest you using your custom format provider something like this, it should solve your problem
class CustomFormatter : IFormatProvider, ICustomFormatter{}
string.Format(new CustomFormatter(), "{0}", yourDecimalValue)
if you want to specify some additional parameters you can pass them to the CustomFormatter ctor. Try playing with this code and I guess you will find exactly what you want.
Thanks, Kiryl
Upvotes: 0