Having created an own number type (actually DoubleDouble
), I want to implement the IFormattable
interface. So I have to somehow parse the format string.
public string ToString(string format, IFormatProvider formatProvider) {
// formatting string according to format and using formatprovider?
return formattedString;
}
The user of the class should be able to use it as a replacement for double
(or any other number format).
String.Format("{0:0.##}", (DoubleDouble)123.4567);
My question is, does someone know a good tutorial about this or can give me some hints? How to support localizing in this process?
How to parse the format string? Are there some methods to aid in this task or do I have to do it all by "hand" with regexp and such?
I really searched for help but couldn't find any, if you find something in another language (C,C++) that may help, please tell me about it.
MSDN has a nice example of a Temperature
class that implements the IFormattable
interface with its own custom format.
I think you know this already; anyway, today I learned that if your DoubleDouble
class implemented the IFormattable
interface then:
String.Format("{0:0.##}", (DoubleDouble)123.4567);
... would call the DoubleDouble
class's ToString(...)
implementation with the specific format "0.##"
as the first parameter, which I suspect is close to what you want. You still have to parse that part of the format though.
I would hazard a guess that much of the format parsing is embedded deep in the highly optimised .Net library binaries, so you don't get any custom parsing virtual methods to help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With