[英]Extra zeroes when parsing string to decimal
I want to parse a string
entered in a WinForms text box as a decimal
.我想将在 WinForms 文本框中输入的string
解析为decimal
。 These values are then stored in a property in an object.然后将这些值存储在 object 的属性中。 They should parsed to two decimal places, exactly like the following examples:它们应该解析到小数点后两位,与以下示例完全相同:
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
How to do this?这个怎么做? At the moment, I am parsing as follows:目前,我正在解析如下:
decimal decimalValue = Decimal.Parse(stringValue);
There are two operations, parsing and rounding.有两种操作,解析和舍入。
decimal decimalValue = Math.Round(Decimal.Parse(stringValue), 2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.