简体   繁体   English

我如何将字符串中的负浮点数转换为C#中的负浮点数?

[英]How i can convert Negative Float with point in String to Negative Float with Point in C#?

I have a String with Negative Float with Point,and it looks like this: 我有一个带负浮点数的字符串,它看起来像这样:

-1.0

And i need to convert it to float,so it need to be a: 而且我需要将其转换为浮点数,因此它必须是:

-1.0f

I tried this: 我尝试了这个:

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

That code is working i think,but it throws a System.FormatException ,so i think i got that exeception,because code cant parse a string with point( . ).I got a exception at float.Parse method. 我认为该代码可以正常工作,但是它抛出System.FormatException ,所以我认为我有这种理解,因为代码无法使用point( . )解析字符串float.Parse方法中出现了异常。

If you want,i will show a full code,where i got error,upper code is just concept of my real code,there is real code: 如果需要,我将显示完整的代码,但我出错了,上面的代码只是我的真实代码的概念,有真实的代码:

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

After some comments,i edited my code to this one: 经过一番评论,我将代码编辑为以下代码:

        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

But i still have FormatException,but now i got it on 但是我仍然有FormatException,但是现在我开始了

float bluee = float.Parse(blue, fmt); //default float of string3

Which is an string with negative number. 这是一个带有负数的字符串。

You need to force the culture info to use a dot for decimals. 您需要强制区域性信息对小数点使用点。 Because your machine's regional settings could be using a comma for decimals. 因为您计算机的区域设置可能使用逗号作为小数点。

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM