Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用点和小数在C中格式化数字#_C#_Asp.net - Fatal编程技术网

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

C# 用点和小数在C中格式化数字#,c#,asp.net,C#,Asp.net,我首先需要。(点)然后是逗号(,) 比如,1234567这是一个数字或金钱的例子 我想要1.234.567,00 谁能给我一个答案。如果执行代码的计算机上的区域性设置符合您的意愿,您只需使用ToString重载即可: double d = 1234567; string res = d.ToString("#,##0.00"); //in the formatting, the comma always represents the group separator and th

我首先需要。(点)然后是逗号(,)

比如,1234567这是一个数字或金钱的例子 我想要1.234.567,00
谁能给我一个答案。

如果执行代码的计算机上的区域性设置符合您的意愿,您只需使用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.
如果显示必须独立于区域性,则可以使用特定的numberformatinfo:

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

您还可以动态更改应用程序的区域性。如果你看一看,看一看“为欧元国家格式化货币”一节,它将详细解释如何做到这一点

基本上,您需要使用以下方法更改区域性:

Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
然后,您可以使用.ToString()方法,将“c”作为参数传递,表示您希望将字符串格式化为当前区域性的货币:

double d = 1234567;
string converted = d.ToString("c");

这会给你你想要的。如果你不想让你正在使用的所有东西都使用欧洲风格的数字,请确保将区域性设置回原位。

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

下面是一个有效的示例:

        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);

xyz=1.234.567000

您是如何在
.ToString()
String.Format(
或类似调用)中调用此更改的?更好的解决方案是更改pc.Convert.ToDecimal(Amount.ToString)(#####0,00)的文化;根据“教人钓鱼”的原则,在MSDN中搜索“双ToString格式”你会发现-然后你需要选择你的格式(具体来说)或格式提供者(例如选择特定的区域文化)。抱歉,我需要点,然后是逗号,如1.234.567,00@Me.name。对于代码中的格式,逗号始终表示分组分隔符,而不是实际显示的字符。上面的第二个代码将始终显示1.234.567,00。如果区域性设置与此对应,则第一个代码也是