c# - 带小数点的 Short.Parse 失败

标签 c# tryparse

我有一个十进制值

decimal myDecimal = 19.0000M;

我正在尝试将其转换为短值。

short newVal;
short.TryParse(myDecimal.ToString(), out newVal); // False

但这失败了。如果我使用 double 就没问题。

为什么会失败?

谢谢

最佳答案

包括文化和数字样式将是更好的方法:

short.TryParse(myDecimal.ToString(), NumberStyles.Integer | NumberStyles.AllowDecimalPoint, 
           System.Globalization.CultureInfo.InvariantCulture, out newVal);

关于c# - 带小数点的 Short.Parse 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40260511/

相关文章:

c# - 为多个 TryParse() 调用重用相同的变量

C#字符串转数字,Parse失败时TryParse返回true

c# - mysql 从存储过程调用系统命令不起作用

c# - 检查列表为空 : not null or use Any 时哪个更好

c# - 并行处理期间的值存储

c# - 在Asp.net中获取两次之间的小时数

c# - 日期不会去Mysql数据库c#

c# - 如何使用 HostBuilder Generic Host ref StopAsync OperationCanceledException 设置 ShutdownTimeout

c# - TryParse 其他类型

c# - Enum 的 TryParse 正在工作,但我认为它不应该