在C#中格式化带点和小数的数字

Kri*_*ish 1 c# asp.net

我需要先.(点)然后逗号(,).

比如,1234567这是一个示例数字或金钱,我希望它像1.234.567,00任何人都可以给我一个答案.

Me.*_*ame 7

如果执行代码的计算机上的文化设置符合您的意愿,您可以简单地使用ToString重载:

    double d = 1234567;
    string res = d.ToString("#,##0.00");  //in the formatting, the comma always represents the group separator and the dot the decimal separator. The format part is culture independant and is replaced with the culture dependant values in runtime.
Run Code Online (Sandbox Code Playgroud)

如果显示必须独立于文化,您可以使用特定的数字格式:

 var nfi = new NumberFormatInfo { NumberDecimalSeparator = ",", NumberGroupSeparator = "." };
    double d = 1234567;
    string res = d.ToString("#,##0.00", nfi); //result will always be 1.234.567,00
Run Code Online (Sandbox Code Playgroud)


Bar*_*est 7

这看起来像外币格式。根据您真正想要的,可能有多种方法可以做到这一点。以下 MSDN 链接为您提供完整文档:

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx#CFormatString

一个有效的示例如下:

        string xyz = "1234567";

        // Gets a NumberFormatInfo associated with the en-US culture.
        NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

        nfi.CurrencyDecimalSeparator = ",";
        nfi.CurrencyGroupSeparator = ".";
        nfi.CurrencySymbol = "";
        var answer = Convert.ToDecimal(xyz).ToString("C3", 
              nfi);
Run Code Online (Sandbox Code Playgroud)

xyz = 1.234.567,000