【问题标题】:int.Parse on negative number results in exceptionint.Parse 负数导致异常
【发布时间】:2023-03-22 11:37:01
【问题描述】:

这行代码:

int.Parse("-1");

当我在设备上运行它时,导致 FormatException 显示“输入字符串的格式不正确”。在模拟器上它工作得很好。我已经用一堆设备对此进行了测试,唯一能按预期工作的设备(即返回值为 -1 的 int)是运行 android 4.2.1 的三星 Galaxy Nexus。所有这些设备都会产生异常:

  • 谷歌 Nexus 7 (4.3)
  • 三星 S3 (4.1.2)
  • 三星 Galaxy Tab (4.0.4)
  • LG P970 (2.2.2)
  • HTC Sensation (2.3.4)

我也试过在设备上从playstore下载appC#Shell,输入上面一行,结果是一样的:

有谁知道为什么会发生这种情况,或者可以做些什么来解决这个问题?

更新:

我正在调查此问题的原因是因为我们的应用程序正在从我们的服务器请求 JSON,并且它包含一堆负数。然后我们使用ServiceStack解析JSON,结果所有负数都变成0。ServiceStack没有办法设置FormatSpecifier。

int.Parse("-1", CultureInfo.InvariantCulture) 似乎可以在所有设备上按预期工作,无论它们使用什么语言,但int.Parse("-1", new CultureInfo("sv-SE")) 不能。

我觉得这有点奇怪,因为 int.Parse("-1", new CultureInfo("sv-SE")) 可以在我们不使用 xamarin 或 mono 的其他平台上工作。

【问题讨论】:

  • 提供 FormatSpecifier 会改变行为吗?我会向 Xamarin 提交错误

标签: c# xamarin.android xamarin


【解决方案1】:

我最终提交了对 ServiceStack 的修复:

https://github.com/ServiceStack/ServiceStack.Text/pull/365

更新:

现在 Xamarin 也修复了它:

https://bugzilla.xamarin.com/show_bug.cgi?id=14185

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2011-06-08
    • 2017-02-16
    • 2018-11-09
    相关资源
    最近更新 更多