Reputation: 576
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);
Upvotes: 0
Views: 59
Reputation: 9482
There are two operations, parsing and rounding.
decimal decimalValue = Math.Round(Decimal.Parse(stringValue), 2);
Upvotes: 4