Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.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#Windows窗体应用程序:Convert.ToDecimal未生成预期结果_C# - Fatal编程技术网

c#Windows窗体应用程序:Convert.ToDecimal未生成预期结果

c#Windows窗体应用程序:Convert.ToDecimal未生成预期结果,c#,C#,我试图对XML文件中的变量值进行十进制。 我使用的代码是: //round monitory values to 2 decimal palaces, even if none are given decimal totalAmount = Math.Round(Convert.ToDecimal(xn.Attributes["totalAmount"].Value), 2); decimal paid = Math.Round(Convert.ToDecimal(xn.Attributes[

我试图对XML文件中的变量值进行十进制。 我使用的代码是:

//round monitory values to 2 decimal palaces, even if none are given

decimal totalAmount = Math.Round(Convert.ToDecimal(xn.Attributes["totalAmount"].Value), 2);
decimal paid = Math.Round(Convert.ToDecimal(xn.Attributes["paidAmount"].Value), 2);
decimal due = Math.Round(Convert.ToDecimal(xn.Attributes["dueAmount"].Value), 2);
它们在XML文件中的值为:

totalAmount="538.0000" 
paidAmount="0.0"
dueAmount="527"
结果是:

totalAmount="538.00"   -  GREAT 
paidAmount="0.0"       -  No Change
dueAmount="527"        -  No Change
我想说的是

totalAmount="538.00" 
paidAmount="0.00"
dueAmount="527.00"
有人能指出我哪里出了问题吗


谢谢

您似乎把舍入和格式混淆了

要获取所需字符串,请执行以下操作:

string totalFormatted = totalAmount.ToString("F");
string paidFormatted = paidAmount.ToString("F");
string dueFormatted = dueAmount.ToString("F");

当它们是十进制时有什么区别?当您尝试将其转换为字符串或打印值的任何位置时,可以在此处强制执行2位小数规则。