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

3 Answers

3

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();

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 !

Just put in your text the float value transformed to int, andthen transformed to string, like this:

YourText.text = Mathf . FloortoInt ( YourFloatnumber ) . ToString ( ) ;

Look: Unity - Scripting API: Mathf.FloorToInt