FormatProvider method and extensions compared to the new class

I wanted to print an integer in Roman numerals and meet this answer from Jesse Slicer. This is an extension method, but I was interested to use ToString(string, IFormatProvider)to do something like

int a = 10;
string b = a.ToString("RN", provider);
// OR
string c = string.Format(provider, "{0:RN} blah foo", a);

instead

int a = 10;
string b = a.ParseRomanNumeral();
// OR
string c = string.Format("{0} blah foo", a.ParseRomanNumeral());

I have never written a format provider, so I'm not sure about the work, but here is my question. For some correct format conversion, such as Roman numerals, you would use:

  • format provider
  • use extension method
  • write a class RomanNumeral that implements Parse, TryParseandToString
  • something else

and why?

string.Format() (, StringBuilder.AppendFormat()) ? , .

, , . , ( ).

+5
3

, , , Format, . int.

+1

, tryparse, parse + tostring(), , ( shahkalpesh .)

+1

, , .

IMO, - . , , # 1 2 ( ).

Expansion methods in both directions will be good.
that is, for numeric types - for converting from a number to Roman and
on string - for converting from Roman to a number (you will need to consider a numeric type that should correspond to the maximum value).

0
source

All Articles