如何将字符串中带点的负浮点转换为C#中带点的负浮点?
我有一个带负浮点数的字符串,它看起来像这样:如何将字符串中带点的负浮点转换为C#中带点的负浮点?,c#,string,exception,runtime-error,formatexception,C#,String,Exception,Runtime Error,Formatexception,我有一个带负浮点数的字符串,它看起来像这样: -1.0 我需要把它转换成float,所以它需要是: -1.0f 我试过这个: bool isNegative = false; //float in my string isnt a negative string mystr = "-1.0"; //my string float myfloat = 0.0f; //my float if(mystr.Contains("-")) { isNegative = true;
-1.0
我需要把它转换成float,所以它需要是:
-1.0f
我试过这个:
bool isNegative = false; //float in my string isnt a negative
string mystr = "-1.0"; //my string
float myfloat = 0.0f; //my float
if(mystr.Contains("-"))
{
isNegative = true;
mystr.Replace("-");
} //if my string is negative,set a bool to true,and remove - from string
if(isNegative==true)
{
myfloat = float.Parse(mystr) * -1.0
} //if bool is true(is says number in string is negative),parse string to float and make it negative
else
{
myfloat = float.Parse(mystr)
} //if bool is false(number in string isnt negative),just parse a number
我想这段代码是有效的,但它抛出了一个System.FormatException
,所以我想我得到了那个异常,因为代码不能解析带点(
)的字符串。我在float.parse
方法中得到了一个异常
如果你愿意,我将展示一个完整的代码,其中我得到了错误,上面的代码只是我真实代码的概念,有真实代码:
bool redNeg = false; //red number isnt negative
bool greenNeg = false; //blue number isnt negative
bool blueNeg = false; //green number isnt negative
string[] args = GetArgs.ExtractArguments(LineText); //get strings from "translate(0.0,0.0,-1.0);"
string red = args[0]; //string1,default is 0.0
string green = args[1]; //string2,default is 0.0
string blue = args[2]; //string3,default is -1.0
if (red.Contains("-"))
{
redNeg = true;
red.Replace("-", "");
} //if string1 is negative,set negative bool to true and remove - from string
if (green.Contains("-"))
{
greenNeg = true;
green.Replace("-", "");
} //if string2 is negative,set negative bool to true and remove - from string
if (blue.Contains("-"))
{
blueNeg = true;
blue.Replace("-", "");
} //if string3 is negative,set negative bool to true and remove - from string
float redd = 0.0f; //default float of string1
float greenn = 0.0f; //default float of string2
float bluee = 0.0f; //default float of string3
if (redNeg==true)
{
redd = float.Parse(red) * -1.0f;
} //if negative bool of string1 is true,set float to negative
else
{
redd = float.Parse(red);
} //if its not,parse it
if (greenNeg == true)
{
greenn = float.Parse(red) * -1.0f;
} //if negative bool of string2 is true,set float to negative
else
{
greenn = float.Parse(green);
} //if its not,parse it
if (blueNeg == true)
{
bluee = float.Parse(red) * -1.0f;
} //if negative bool of string3 is true,set float to negative
else
{
bluee = float.Parse(blue);
} //if its not,parse it
gl.Translate((float)redd, (float)greenn,(float) bluee); //render function,dont touch it
在一些评论之后,我将代码编辑为:
var fmt = new NumberFormatInfo();
fmt.NegativeSign = "−";
//LineText is "translate(0.0,0.0,-1.0);
string[] args = GetArgs.ExtractArguments(LineText); //get strings from "translate(0.0,0.0,-1.0);"
string red = args[0]; //string1,default is 0.0
string green = args[1]; //string2,default is 0.0
string blue = args[2]; //string3,default is -1.0
float redd = float.Parse(red,fmt); //default float of string1
float greenn = float.Parse(green, fmt); //default float of string2
float bluee = float.Parse(blue, fmt); //default float of string3
gl.Translate((float)redd, (float)greenn,(float) bluee); //render function,dont touch it
但我还是有例外,但现在我明白了
float blue=float.Parse(blue,fmt)//string3的默认浮点值
这是一个带负数的字符串。您需要强制区域性信息使用小数点。 因为您机器的区域设置可能使用逗号表示小数
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
System.Globalization.NumberFormatInfo ni = (System.Globalization.NumberFormatInfo)ci.NumberFormat.Clone();
ni.NumberDecimalSeparator = ".";
string s = "-1.0";
var result = float.Parse(s, ni);
您需要强制区域性信息使用小数点。 因为您机器的区域设置可能使用逗号表示小数
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.InstalledUICulture;
System.Globalization.NumberFormatInfo ni = (System.Globalization.NumberFormatInfo)ci.NumberFormat.Clone();
ni.NumberDecimalSeparator = ".";
string s = "-1.0";
var result = float.Parse(s, ni);
为什么需要
f
?只有在定义浮点变量时,在解析其隐含的字符串时,才需要这样做。另外,您的格式设置也很糟糕。请参见gl.translate方法,因此该方法只接受float类型,带有f
。究竟哪个float.Parse
调用引发了所述异常?你贴的是哪一行?很不清楚你的问题是什么float.Parse(“-1.0”)
将正常工作,并完全按照您的期望执行(即解析字符串并输出一个值为-1的float)。如果您在float.Parse中遇到问题,那么您需要准确地向我们显示您传递给解析方法的导致问题的输入,然后我们可能能够帮助您诊断问题。如果您不知道要传递给float.Parse的值是什么,那么您真的需要研究如何使用调试器来查找此信息。@meowgoestedoge:float.Parse(-1.0”)
将正确解析,所以空格不应该是问题。为什么需要f
?只有在定义浮点变量时,在解析其隐含的字符串时,才需要这样做。另外,您的格式设置也很糟糕。请参见gl.translate方法,因此该方法只接受float类型,带有f
。究竟哪个float.Parse
调用引发了所述异常?你贴的是哪一行?很不清楚你的问题是什么float.Parse(“-1.0”)
将正常工作,并完全按照您的期望执行(即解析字符串并输出一个值为-1的float)。如果您在float.Parse中遇到问题,那么您需要准确地向我们显示您传递给解析方法的导致问题的输入,然后我们可能能够帮助您诊断问题。如果您不知道要传递给float.Parse的值是什么,那么您真的需要研究如何使用调试器来查找此信息。@meowgoestedoge:float.Parse(-1.0”)
将正确解析,所以空格不应该是问题。哦,我的上帝,它最终会工作的,非常感谢您,RENO@Reno:你能解释一下实际的问题是什么,以及为什么这个答案解决了这个问题吗?有了解释,你的答案会更好!添加了一些评论。哦,我的上帝,它的最终作品,非常感谢你,RENO@Reno:你能解释一下实际的问题是什么,以及为什么这个答案解决了这个问题吗?有了解释,你的答案会更好!添加了一些评论。