我想将在 WinForms 文本框中输入的 string
解析为 decimal
。然后这些值存储在对象的属性中。它们应该解析为两位小数,与以下示例完全相同:
string amount1 = 2; // should be parsed as 2.00
string amount2 = 2.00; // should be parsed as 2.00
string amount3 = 2.5; // should be parsed as 2.50
string amount4 = 2.50; // should be parsed as 2.50
string amount5 = 2.509; // should be parsed as 2.51
如何做到这一点?目前,我解析如下:
decimal decimalValue = Decimal.Parse(stringValue);
答案 0 :(得分:4)
有两种操作,解析和舍入。
decimal decimalValue = Math.Round(Decimal.Parse(stringValue), 2);