【问题标题】:Converting string value to float value将字符串值转换为浮点值
【发布时间】:2020-08-23 20:03:31
【问题描述】:

在我的代码中,我将字符串值转换为浮点值。在我的代码中:guns - 数组(大小 = 5)和 Chargunsinformations - 字符串值。

guns[gunsNum] = float.Parse(ChargunsInformation);

Unity 给我一个错误:

FormatException:输入字符串的格式不正确。 System.Number.ParseSingle(System.String 值, System.Globalization.NumberStyles 选项, System.Globalization.NumberFormatInfo numfmt) (在 :0) System.Single.Parse (System.String s,System.Globalization.NumberStyles 样式, System.Globalization.NumberFormatInfo 信息)(在 :0) System.Single.Parse (System.String s,System.IFormatProvider 提供程序)(在 :0) System.Convert.ToSingle (System.String 值)(在 :0) StartOfAllScript.Update () (在 Assets/Scripts/Player 脚本/StartOfAllScript.cs:35)

请帮帮我。我试图解决这个问题 2 小时。

【问题讨论】:

  • ChargunsInformation 的价值是什么?为什么要为这个问题添加p 标签?你可以试试float.TryParse() 而不是float.parse()

标签: c#


【解决方案1】:

我想问题与您的系统文化有关。请尝试以下代码 sn-p

guns[gunsNum] = float.Parse(ChargunsInformation, CultureInfo.InvariantCulture);

【讨论】:

  • Assets\Scripts\Player Scripts\StartOfAllScript.cs(34,66): error CS0103: The name 'CultureInfo' does not exist in the current context to give me unity
  • 你添加了 System.Globalization 命名空间吗?您能否提供您想要转换的价值以及您的系统当前文化?
  • 是的,我添加了。我想将 Chargunsinformations 转换为 guns 数组的值
  • 您要转换的“Chargunsinformations”变量中的值是多少?您能否提供您的系统当前文化。那么,重现问题有帮助吗?
  • Chargunsinformations - 我要转换的字符串值。我的文化是 ru-RU
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多