问题描述
decimal decimalVal;
Decimal.TryParse("123-", out decimalVal);
Console.WriteLine(decimalVal); // -123
为什么用这种方式解析"123-"字符串?
Why do "123-" string parsed this way?
推荐答案
十进制.TryParse方法使用 NumberStyles.Number 默认情况下. NumberStyles.Number 包括 NumberStyles.AllowTrailingSign .
The Decimal.TryParse Method parses the input with NumberStyles.Number by default. NumberStyles.Number includes NumberStyles.AllowTrailingSign.
[...]
参数s使用NumberStyles.Number样式进行解释.
[...]
[...]
Parameter s is interpreted using the NumberStyles.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.
这篇关于十进制.解析字符串,后缀减号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!