decimal decimalVal;
Decimal.TryParse("123-", out decimalVal);
Console.WriteLine(decimalVal); // -123
Why do "123-" string parsed this way?
Converting a string to a decimal value or decimal equivalent can be done using the Decimal. TryParse() method. It converts the string representation of a number to its decimal equivalent.
Converts the string representation of a number to its Decimal equivalent.
The Decimal.TryParse Method parses the input with NumberStyles.Number by default. NumberStyles.Number includes NumberStyles.AllowTrailingSign.
Decimal.TryParse Method (String, Decimal)
[...]
Parameter s is interpreted using the NumberStyles.Number style.
[...]
Number Indicates that the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles are used. This is a composite number style.
AllowTrailingSign Indicates that the numeric string can have a trailing sign. Valid trailing sign characters are determined by the NumberFormatInfo.PositiveSign and NumberFormatInfo.NegativeSign properties.
NumberStyles.Number enumerator is used by default:
Indicates that the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles are used. This is a composite number style
http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With