UI Text to Decimal. How?
So this is my code, my input fields are supposed to have decimal numbers, that part does work. I need to convert the text showing into a decimal as well, not a string, so that I can later add a part of the code that increases the number that was written at first. I've tried a lot of tricks, but none seem to work. Is there a way to convert the Text to a decimal? P.S I am using System
public class GameManager : MonoBehaviour {
public InputField Jackpot1;
public Text Jack1;
public InputField Jackpot2;
public Text Jack2;
public InputField Jackpot3;
public Text Jack3;
public void CopyText()
{
Jack1.text = Jackpot1.text;
Jack2.text = Jackpot2.text;
Jack3.text = Jackpot3.text;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Convert.ToDecimal (Jack1);
}
}
Answer by Cepheid · Nov 19, 2016 at 01:28 AM
C#'s datatypes have a built in function that allows you to parse string data into int's, float's and double's. Simply check out the Parse() and TryParse() methods.
Double.TryParse Example - MSDN Docs
Float.Parse Example - MSDN Docs
These methods allow you to convert a string into a number type. Parse will attempt to parse the string and if it fails it will throw a FormatException. If you want to perform some simple exception handling then TryParse will allow you test if the parse was successful.
Answer by MusicKing7 · Nov 19, 2016 at 02:35 AM
float.Parse("Text name".text)
This turns the text from your text component into a float.
Answer by EvalDaemon · Jan 17 at 04:25 AM
How the hell does these WRONG solutions come up at the top of the search.
It's
string s = "-3.14";
float f = 0f;
if (float.TryParse(s, out f))
{
Debug.Log("results= " + f );
}
else Debug.Log("Failed to Parse.");
The first answer I guess isn't WRONG, but is referencing .NET6 and has broken links. The second isn't really much of an answer.