简体   繁体   中英

Format Financial Data like “2,000” as “2M” using a custom Format String with C# .NET WinForms and WPF Applications

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 IFormatProvider and ICustomFormatter and pass this into the Format() and ToString() calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM