输入的字符串格式不正确-为什么我得到这个? [英] input string was not in a correct format - Why am I getting this?
本文介绍了输入的字符串格式不正确-为什么我得到这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在对某些库进行编码时,我的字符串格式遇到了一些错误.
我真的不知道如何解决这个问题,或者关于字符串格式化的任何事情.
我的代码:
While coding some libraries I ran into some errors with my string format.
I don''t really know how to solve this, or anything about string formatting.
My Code:
public float getMeleeCB(float attk, float str, float def, float hp, float pray)
{
return (Convert.ToSingle((double)Convert.ToDecimal("0.3f") * (1.3 * (attk + str) + def + hp + (.5 * pray))));
//example of use: string test = "Melee Combat Level: "+getMeleeCB(60,53,58,53,47).ToString();
}
运行时错误消息:
未处理Format-Exception-输入字符串的格式不正确
Run-Time Error Message:
Format-Exception was unhandled - input string was not in a correct format
How do I format it to work the way I set it up?
推荐答案
附加调试器,您将发现某些参数不是正确的浮点值" .
您应该使用 TryParse [
Attach a debugger and you will find that some of your parameters isn''t a correct float value.
You should use TryParse[^] if you are not sure about that the inputs are validated.
Hope this helps!
这部分将始终抛出异常:Convert.ToDecimal("0.3f")
.0.3f
是编译器可以识别的,但不应在代码中使用.其他内容似乎还可以.
This part will always throw an exception:Convert.ToDecimal("0.3f")
. The0.3f
is something the compiler recognizes, but should not be used in the code.Other stuff seems to be ok.
这篇关于输入的字符串格式不正确-为什么我得到这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文