Format Exception when trying to parseFloat(String)
Hello, I have a problem with my script. For some reason i get a Format Exception when i try to parseFloat() a String that i get from an InputField.
The Exception:
FormatException: Invalid format. System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Double.cs:209) System.Single.Parse (System.String s) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Single.cs:183) UnityScript.Lang.UnityBuiltins.parseFloat (System.String value) ButtonScript.Calc () (at Assets/Scripts/ButtonScript.js:61) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
The Code:
"#pragma strict
import UnityEngine.UI;"
//INPUT STRINGS
var input1 : String;
var input2 : String;
var input3 : String;
var input4 : String;
//INPUT FIELDS
var inputF1 : InputField; //input Field 1
var inputF2 : InputField; //__""__ 2
var inputF3 : InputField; //...
var inputF4 : InputField; //...
var result : Text;
private final var R : float = 6371000; //Earth's radius in metre
//INPUT VARIABLES
var d : float; //distance to travel
var a1 : float; //latitude in dezimals
var b1 : float; //longitude in dezimals
var brng : float; //Angle you are going to
function Start () {
}
function Update () {
}
//
//BUTTON FUNCTIONS
//
function ExitButton(){
Application.Quit();
}
function InputF1(){
input1 = inputF1.text;
}
function InputF2(){
input2 = inputF2.text;
}
function InputF3(){
input3 = inputF3.text;
}
function InputF4(){
input4 = inputF4.text;
}
function Calc(){
//aFloat = parseFloat(input1); I TRIED ALL THIS BEVORE WIHT ALL
//bFloat = parseFloat(input2); SIMPLE TEST. IT WORKED BACK THEN.
//flaeche = aFloat * bFloat;
//result.text = ""+surface;
d = parseFloat(input3);
a1 = parseFloat(input1);
b1 = parseFloat(input2);
brng = parseFloat(input4);
var a2 = Mathf.Asin( Mathf.Sin(a1)*Mathf.Cos(d/R) +
Mathf.Cos(a1)*Mathf.Sin(d/R)*Mathf.Cos(brng) );
var b2 = b1 + Mathf.Atan2(Mathf.Sin(brng)*Mathf.Sin(d/R)*Mathf.Cos(a1),
Mathf.Cos(d/R)-Mathf.Sin(a1)*Mathf.Sin(a2));
result.text = a2+"°N "+b2+"°W ";
}
//
//BUTTON FUNCTIONS END
//
I have no idea what this Exception is telling me. I am a more or less beginner programmer so please don´t be to harsh on me if the structure of the code is not that good.
The "result" variable is a Text variable which gets displayed in the UI.
Here a picture of the UI. (sry for it beeing German but i think that does not matter that much anyway.)
Thanks in Advance! Lucas
Answer by DonkeyMalonkey · Jul 15, 2016 at 05:46 AM
Anyone? Could rly use a tip. Maybe i missed something? I have attached everything in the Inspector, so that´s not it.
EDIT WOW I am so stupid haha.. I copied the Input Fields so they all still had the first input field function. Thanks for the help anyways, I guess. Took me a few hours to find that stupid mistake but i did find it after all.
Maybe this post helps someone in the future.
Answer by _Yash_ · Jul 14, 2016 at 10:59 AM
try float.Parse instead you can also use float.tryParse to handle invalid input
float converted = float.Parse("17463274");
Ok, will try that right away. That´s c++ right? Does not matter anyway just curious :).
Still got the same Exception with "float.Parse(someString);" As for float.tryParse, that does not work either. Or did you mean float.TryParse. But that does not work either.
Your answer
Follow this Question
Related Questions
Combining multiple strings in Javascript 1 Answer
Help int.Parse "Input String was not in the correct format" PROBLEM 0 Answers
A certain string is causing trouble! (Javascript) 0 Answers
Error FormatException: Input string was not in a correct format. 0 Answers
Parse string value to add to enum List 2 Answers