C# 字符串格式数字 千 123K、百万 123M、十亿 123B

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11731996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:59:49  来源:igfitidea点击:

String Format Numbers Thousands 123K, Millions 123M, Billions 123B

c#string-formatting

提问by Mike U

Is there a way using a string formatter to format Thousands, Millions, Billions to 123K, 123M, 123B without having to change code to divide value by Thousand, Million or Billion?

有没有办法使用字符串格式化程序将千、百万、十亿格式化为 123K、123M、123B,而无需更改代码以将值除以千、百万或十亿?

String.Format("{0:????}", LargeNumber)

采纳答案by Daniel

There are different ways to achieve this, but for me the easiest and quickest is to use the "," custom specifier

有不同的方法可以实现这一点,但对我来说最简单快捷的是使用“,”自定义说明符

double value = 1234567890;

// Displays 1,234,567,890   
Console.WriteLine(value.ToString("#,#", CultureInfo.InvariantCulture));

// Displays 1,234,568K
Console.WriteLine(value.ToString("#,##0,K", CultureInfo.InvariantCulture));

// Displays 1,235M
Console.WriteLine(value.ToString("#,##0,,M", CultureInfo.InvariantCulture));

// Displays 1B
Console.WriteLine(value.ToString("#,##0,,,B", CultureInfo.InvariantCulture));

回答by dtb

You can implement a ICustomFormatterthat divides the value by thousand, million or billion, and use it like this:

您可以实现一个ICustomFormatter将值除以千、百万或十亿,并像这样使用它:

var result = string.Format(new MyCustomFormatter(), "{0:MyFormat}", number);

回答by RAM

I use this mix of formats in an Extension Method(just add it to your project and enjoy)

我在扩展方法中使用这种混合格式(只需将其添加到您的项目中即可享受)

public static string ToKMB(this decimal num)
{
    if (num > 999999999 || num < -999999999 )
    {
        return num.ToString("0,,,.###B", CultureInfo.InvariantCulture);
    }
    else
    if (num > 999999 || num < -999999 )
    {
        return num.ToString("0,,.##M", CultureInfo.InvariantCulture);
    }
    else
    if (num > 999 || num < -999)
    {
        return num.ToString("0,.#K", CultureInfo.InvariantCulture);
    }
    else
    {
        return num.ToString(CultureInfo.InvariantCulture);
    }
}

Use:

用:

((decimal)235).ToKMB();
// 235

((decimal)1235).ToKMB();
// 1.2K

((decimal)6271235).ToKMB();
// 6.27M

((decimal)93246571235).ToKMB();
// 93.247B

Notes:

  • It return more detail for bigger numbers and I like it.

  • It support negative numbers too. (Thanks to @Dusty for his note in comments.

  • I write a method for decimalnumbers in this example, you can write some override methods for it to support int, longand doubleto use it without any casting such as:

    myIntNumber.ToKMB();

    myLongNumber.ToKMB();

    myDoubleNumber.ToKMB();

    myDecimalNumber.ToKMB();

笔记:

  • 它为更大的数字返回更多细节,我喜欢它。

  • 它也支持负数。(感谢@Dusty 在评论中的注释。

  • decimal在这个例子中写了一个数字方法,你可以写一些覆盖方法来支持它intlongdouble在不进行任何转换的情况下使用它,例如:

    myIntNumber.ToKMB();

    myLongNumber.ToKMB();

    myDoubleNumber.ToKMB();

    myDecimalNumber.ToKMB();