gpt4 book ai didi

c# - 十进制。解析字符串,后缀为减号

转载 作者:可可西里 更新时间:2023-11-01 08:24:01 24 4
gpt4 key购买 nike

decimal decimalVal;
Decimal.TryParse("123-", out decimalVal);
Console.WriteLine(decimalVal); // -123

为什么“123-”字符串要这样解析?

最佳答案

Decimal.TryParse MethodNumberStyles.Number 解析输入默认。 NumberStyles.Number包括 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.

关于c# - 十进制。解析字符串,后缀为减号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17147919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com