Formatting Large Numbers with .NET

前端 未结 5 1804
梦如初夏
梦如初夏 2020-12-16 03:45

I have a requirement to format large numbers like 4,316,000 as \"4.3m\".

How can I do this in C#?

5条回答
  •  失恋的感觉
    2020-12-16 04:32

    If you're only running on Windows you could use a p-invoke declaration in C# or VB.NET to call the Win32 functions StrFormatByteSizeW or StrFormatByteSize64. If your application/site is guaranteed to be running on at least Vista SP1 or Server 2008 there's also StrFormatByteSizeEx with a few more options.

    Sample from the MSDN docs:

    Numeric value   Text string 
    532             532 bytes 
    1340            1.30KB 
    23506           22.9KB 
    2400016         2.29MB 
    2400000000      2.23GB 
    

    These APIs also handle localization correctly for non-English users.

提交回复
热议问题