Decimal. Parse string, postfixed by a minus sign

后端 未结 4 922
栀梦
栀梦 2021-01-17 12:01
decimal decimalVal;
Decimal.TryParse(\"123-\", out decimalVal);
Console.WriteLine(decimalVal); // -123

Why do \"123-\" string parsed this way?

4条回答
  •  心在旅途
    2021-01-17 12:45

    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.

提交回复
热议问题