- Home /
Possible to convert a Float into an Int?
Hello,
I have a value that is being passed onto another script in a float format. I want to place this value as a GUI text, but as its in a float, I get a load of ugly numbers after a decimal. How can I convert this into an Int (or a solid number).
I dont want to change the value variable from : float it :int. This is out of the question. Is there another way?
Thanks
Already asked/answered: http://answers.unity3d.com/questions/50864/can-i-shorten-the-time-on-the-gui/50865#50865
Answer by Peter G · Apr 07, 2011 at 02:04 AM
I'm not sure why you can't convert it to an int variable, you could do some thing like:
//C#
GUILabel.text = ((int)floatValue).ToString();
Answer by letmeknowit · Mar 08, 2012 at 12:15 AM
Or u can use a trick and try this:
//C# GUILabel.text = ""+(int)floatValue);
all values added after string --> "string" <--- are always converted to string !
Answer by tormentoarmagedoom · Jun 19, 2016 at 05:04 AM
Just put in your text the float value transformed to int, andthen transformed to string, like this:
YourText.text = Mathf . FloortoInt ( YourFloatnumber ) . ToString ( ) ;
Look: https://docs.unity3d.com/ScriptReference/Mathf.FloorToInt.html