I want to parse a string
entered in a WinForms text box as a decimal
. These values are then stored in a property in an 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.