[英]Extra zeroes when parsing string to decimal
我想將在 WinForms 文本框中輸入的string
解析為decimal
。 然后將這些值存儲在 object 的屬性中。 它們應該解析到小數點后兩位,與以下示例完全相同:
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);
有兩種操作,解析和舍入。
decimal decimalValue = Math.Round(Decimal.Parse(stringValue), 2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.