Reputation: 1543
When ever one passes string with suffix parsing to decimal
fails.
decimal testValue;
decimal.TryParse("5M", NumberStyles.Number, CultureInfo.CurrentCulture, out testValue)
Following parse will return false
.
Why does TryParse
fail when you pass in a string with a suffix?
Upvotes: 5
Views: 417
Reputation: 98810
Because there is no way to parse your string without removing M
part. And none of NumberStyles
have such a functionality.
I can suggest to Replace
your M
with empty string but that would be only solve for your case, it won't be a general solution.
decimal testValue;
decimal.TryParse("5M".Replace("M", ""), NumberStyles.Number,
CultureInfo.CurrentCulture, out testValue);
A real-type-suffix specifies number types. It teaches to C# compiler what a numeric literal type considered as. In a string, it means nothing. It is just an another character.
Upvotes: 0
Reputation: 11095
Because Decimal.TryParse
does not support it.
Depending on the value of style, the s parameter may include the following elements:
[ws][$][sign][digits,]digits[.fractional-digits][e[sign]digits][ws]
Elements in square brackets ([ and ]) are optional. The following table describes each element.
ws
: Optional white space. White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag. It can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$
: A culture-specific currency symbol. Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern or NumberFormatInfo.CurrencyPositivePattern properties of the NumberFormatInfo object returned by the IFormatProvider.GetFormat method of the provider parameter. The currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
sign
: An optional sign.
digits
: A sequence of digits ranging from 0 to 9.
.
: A culture-specific decimal point symbol.
fractional-digits
: A sequence of digits ranging from 0 to 9.
Upvotes: 4