十进制.解析字符串,后缀减号 [英] Decimal. Parse string, postfixed by a minus sign
问题描述
decimal decimalVal;
Decimal.TryParse("123-", out decimalVal);
Console.WriteLine(decimalVal); // -123
为什么用这种方式解析"123-"字符串?
Why do "123-" string parsed this way?
推荐答案
十进制.TryParse方法使用 NumberStyles.Number 默认情况下. NumberStyles.Number 包括
The Decimal.TryParse Method parses the input with NumberStyles.Number by default. NumberStyles.Number includes NumberStyles.AllowTrailingSign.
Decimal.TryParse方法(字符串,十进制)
[...]
参数s使用NumberStyles.Number样式进行解释.
[...]
[...]
Parameter s is interpreted using the NumberStyles.Number style.
[...]
数字指示使用了AllowLeadingWhite,AllowTrailingWhite,AllowLeadingSign,AllowTrailingSign,AllowDecimalPoint和AllowThousands样式.这是一种复合数字样式.
Number Indicates that the AllowLeadingWhite, AllowTrailingWhite, AllowLeadingSign, AllowTrailingSign, AllowDecimalPoint, and AllowThousands styles are used. This is a composite number style.
AllowTrailingSign 表示数字字符串可以带有结尾符号.有效的尾随符号由NumberFormatInfo.PositiveSign和NumberFormatInfo.NegativeSign属性确定.
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.
这篇关于十进制.解析字符串,后缀减号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!