We’d like to know if there is a way to format financial data such as “2,000” and “2,000,000” as “2M” and “2MM” respectively – essentially replacing the 1000’s with M’s.
Ideally there would be a format string we could use, such that 2000.0.ToString(“X”) would give us “2M” back and vice versa. Does such a formatstring exist? If not – is it possible to create one?
Note that these format strings work:
#,#,M – this will convert 2,000 to 2M (and back)
#,#,,MM – this will convert 2,000,00 to 2MM (and back)
#,#,,,B – this will convert 2,000,000,000 to 2B (and back)
However we need something more dynamic – something that can detect if it’s any of these values and can apply the appropriate format string, if possible.
I don’t know of a format to achieve what you want. You can, however, implement your own with the interfaces
IFormatProviderandICustomFormatterand pass this into theFormat()andToString()calls.