FormatException: Input string was not in a correct format

here’s my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

 public class TestConversion : MonoBehaviour
 {
     string s = "(0.0,0.0,0.0)";

     // Update is called once per frame
     void Update()
     {
        if(Input.GetKeyDown("q"))
         Debug.Log(StringToVector3(s));
     }
 
    public static Vector3 StringToVector3(string sVector)
         {
      // Remove the parentheses
      if (sVector.StartsWith ("(") && sVector.EndsWith (")")) 
    {
       sVector = sVector.Substring(1, sVector.Length-2);
    }
      
    // split the items
    string[] sArray = sVector.Split(',');
      
     // store as a Vector3
      Vector3 result = new Vector3(
      float.Parse(sArray[0]),
      float.Parse(sArray[1]),
      float.Parse(sArray[2]));
      
    return result;
          }
 
 
 }

it’s attached to a gameobject, and this error message appears in the console after i press q:

FormatException: Input string was not in a correct format.
System.Number.ThrowOverflowOrFormatException (System.Boolean overflow, System.String overflowResourceKey) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Number.ParseSingle (System.ReadOnlySpan`1[T] value, System.Globalization.NumberStyles styles, System.Globalization.NumberFormatInfo info) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
System.Single.Parse (System.String s) (at <e40e5a8f982c4b618a930d29f9bd091c>:0)
TestConversion.StringToVector3 (System.String sVector) (at Assets/scripts/TestConversion.cs:28)
TestConversion.Update () (at Assets/scripts/TestConversion.cs:13)

i have no clue what might be causing this error. please help.

float.Parse uses your systems culture settings for the decimal seperator. You can pass CultureInfo as a second parameter to avoid this.

float number = float.Parse(sNumber, CultureInfo.InvariantCulture);

Hope this helps!

Hello,

It is a common problem when parsing float and double variable. Problem lies with separators like “,” and “.”. Cultures have different separators USA uses “.”, but Spain “,” etc.

Solution is to make parsing invariant of the culture through additional parameter of float.Parse.

Vector3 result = new Vector3(
       float.Parse(sArray[0], CultureInfo.InvariantCulture),
       float.Parse(sArray[1], CultureInfo.InvariantCulture),
       float.Parse(sArray[2], CultureInfo.InvariantCulture));

god damnit, that actually worked.
why would anyone make it work like this? i’m actually curious, i never encountered anything like this