Nintenz0
Nintenz0

Reputation: 23

How to get a String to Float with Conversion and use Them for Cordinates

Hello Community im trying to learn C# but this Problems makes me Crazy, Im Trying to Read Cordinates from a TXT File and split them into 3. And then im tried to convert them to Float because for Cordinates i need Float Right?

Code First Try

            foreach (string CORDS in System.IO.File.ReadLines(@"/RAGEMP/server-files/blips/hospital/hospitalcords.txt"))
        { 
            float[] floatData = Array.ConvertAll(CORDS.Split(','), float.Parse);
            float float1 = floatData[0];
            float float2 = floatData[1];
            float float3 = floatData[2];
            Blip Hospital = NAPI.Blip.CreateBlip(153, new Vector3(float1,float2,float3), 0.5f, 1, "Hospital"); ;
            NAPI.Blip.SetBlipShortRange(Hospital, true);
        }

Second Try

            foreach (string CORDS in System.IO.File.ReadLines(@"/RAGEMP/server-files/blips/hospital/hospitalcords.txt"))
        {
            string[] split = CORDS.Split(',');
            float flt1 = float.Parse(split[0]); 
            float flt2 = float.Parse(split[1]);
            float flt3 = float.Parse(split[2]);
            Blip Hospital = NAPI.Blip.CreateBlip(153, new Vector3(flt1, flt2, flt3), 0.5f, 1, "Hospital"); ;
            NAPI.Blip.SetBlipShortRange(Hospital, true);
        }

but not one of them worked and im trying to Convert them into a Float was because im Trying to Learn C# with a Framework (RageMP) for GTA 5 Servers and the Vector needs cordinates and with a String i cant use them. (Please be friendly to a beginner im started last Week :D)

Text from the readed File

290.8559, -589.8578, 42.764145

Picture of the Warning: enter image description here

Upvotes: 2

Views: 122

Answers (1)

Mucksh
Mucksh

Reputation: 160

The parsing it self should work fine - tried your code - but you may need to specify a CultureInfo try

float.Parse(split[0],CultureInfo.InvariantCulture)

Parsing in .net unfortunally usually localized and dependent on language of the user that runs the program e.g. for me or you with german system language it will only parse numbers using "," as decimal point right

Maybe thats your problem

if not maybe try

   var cordList = text.Split(',').Select(item => float.Parse(item.Trim(),CultureInfo.InvariantCulture)).ToList();
   var pos = new Vector3(cordList[0], cordList[1], cordList[2]);

not sure if the left over spaces could result in errors while parsing

Upvotes: 1

Related Questions