I'm working on a Complex number struct.
To display the numbers I've overridden the ToString method like this:
public override string ToString()
{
if (_imag >= 0)
return String.Format("({0}+{1}i)", _real, _imag);
else //imaginary part negative
return String.Format("({0}{1}i)", _real, _imag);
}
Works fine. My numbers get displayed as (a+bi) or (a-bi).
But I like also to implement the j format : a+bj or the ordered pair format : (a, b)
I know that I have to derive from IFormattable to do something like that. Looked here on daniweb and googled but did not find much info on how to do it.
I think I must implement public string ToString(string format, IFormatProvider formatProvider) and use a switch case to choose the format string. But it is all a bit hazy...
My questions :
Can I freely choose the format strings, even non standard ones?
Must I use IFormatProvider?