Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Input string was not in a correct format

Discussion in 'Scripting' started by SrPatolino_, May 4, 2020.

  1. SrPatolino_

    SrPatolino_

    Joined:
    Nov 5, 2019
    Posts:
    9
    I'm trying to create a calculator but this is giving this error when I'm going to save
    FormatException: Input string was not in a correct format.
    System.Number.ParseSingle (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at <437ba245d8404784b9fbab9b439ac908>: 0)
    System.Single.Parse (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <437ba245d8404784b9fbab9b439ac908>: 0)
    System.Single.Parse (System.String s) (at <437ba245d8404784b9fbab9b439ac908>: 0)
    Calculadora.Update () (at Assets / Calculadora.cs: 54)

    I already tried it in different ways and I couldn't solve it

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class Calculadora : MonoBehaviour
    {
    public InputField num1;
    public InputField num2;
    public InputField op;
    public InputField SalvaResultado;
    public Text salvo;
    public Text output;
    float resultado;
    float salvar;

    // Update is called once per frame
    void Update()
    {

    if (Input.GetKeyDown("enter"))
    {
    switch (op.text)
    {
    case "+":
    resultado = float.Parse(num1.text) + float.Parse(num2.text);
    output.text = resultado.ToString();
    break;

    case "-":
    resultado = float.Parse(num1.text) - float.Parse(num2.text);
    output.text = resultado.ToString();
    break;

    case "*":
    resultado = float.Parse(num1.text) * float.Parse(num2.text);
    output.text = resultado.ToString();
    break;


    case "/":
    resultado = float.Parse(num1.text) / float.Parse(num2.text);
    output.text = resultado.ToString();
    break;

    default:
    break;
    }

    }

    if (Input.GetKeyDown("s"))
    {
    salvar = float.Parse(SalvaResultado.text);
    salvo.text = salvar.ToString();
    salvo.text = output.text;
    }
    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,143
    Please use code tags when you post long bits of code.

    Otherwise, the error is pretty clear. When you do a float.Parse call, the value you pass in has to be able to be converted to a float. So it's either your num1.text, num2.text, or SalvaResultado.text values that aren't in a format that can be changed. What values are you entering?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Everything that @Brathnann said above, plus if you like you can use the float.TryParse call if you expect invalid data.

    https://docs.microsoft.com/en-us/dotnet/api/system.single.tryparse?view=netcore-3.1

    float
    is simply an alias for
    System.Single
    , which is a single-precision floating point number. You can write it with float:

    Code (csharp):
    1. float result = 0.0f;
    2.  
    3. string MyInputString = " ....";
    4.  
    5. if (float.TryParse( MyInputString, out result))
    6. {
    7.   // use your valid result here
    8. }
     
    brian-nielsen likes this.
  4. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Hey there,

    I have to necro this, since I'm experiencing the same issue. It specifically happens when I'm trying to delete text inside a GUILayout.TextField in OnGUI(), which auto updates. It doesn't break anything, and automatically replaces the "invalid" argument with a valid number.

    So I think this is an engine problem. It trys to parse something, that you took away from it.

    Still I don't need that exception report, if the engine fixes itself.
     
  5. brian-nielsen

    brian-nielsen

    Joined:
    Apr 18, 2018
    Posts:
    15
    I noticed I had this same issue when I switched from a standard Unity UI Input Field to a Text Mesh Pro Input Field.

    That being said @Kurt-Dekker 's solution worked to fix my issue. Thank you very much :)
     
    Kurt-Dekker likes this.