c#Windows窗体应用程序:Convert.ToDecimal未生成预期结果
我试图对XML文件中的变量值进行十进制。 我使用的代码是: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[
//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位小数规则。