how can i convert a float value to integer to display in gui.label. using cs. C#
untested but should work:
int myBlubb = myFloatBlubb as int;
or
int myBlubb = (int) myFloatBlubb;
or just the right side when setting the label text.
If you want to totally overcontrol it, you can also use String.Format …
float fValue = 0.123456f;
int iValue = (int)fValue;
Debug.Log("int val: " +iValue);
iValue = Mathf.FloorToInt(fValue);
Debug.Log("int val: " +iValue);
iValue = Mathf.CeilToInt(fValue);
Debug.Log("int val: " +iValue);
iValue = Mathf.RoundToInt(fValue);
Debug.Log("int val: " +iValue);
And there are most likely more ways.
//Perlohmann
I still got one
float myFloat = 3.5F;
int myInt = Convert.ToInt32(myFloat);
The difference between the casts ((int)myFloat vs. myFloat as int) usually is that using “as” will return null if there is no cast possible while “(int)” will throw an exception when no cast is possible. I don’t know for sure how that behaves for ints for which there is no such thing as null as it’s a value type - might be you get “0” instead when you use the “as” operator - or it throws some sort of (compiler-)error (haven’t tested it myself, since the documentation of the as operator says it requires a reference type, I guess you will get an error).
Some interesting references:
Why Convert.ToInt32 Might be Dangerous
Is casting the same thing as converting?
Casting and Type Conversions
Anyways, I’d use the Mathf-methods that Perlohmann suggested as they are easiest to read and understand (you can easily see what the code does by just reading the method names instead of having to know all the details of casting and converting).
so this is VERY late answer but if someone is still looking for easiest way to accomplish this here:
floatVariable.ToString ("0")
this displays only full numbers, if you put “0.0” instead it would display numbers with just one decimal etc…
To complete on what Jashan said, using as with a non nullable value (int, float, etc…) will throw a compiler error, but using nullable type created from value type (int?, float?, etc…) will also throw a compiler error.
Well, at least you’re necro’ing a timeless thread…
Also, keep in mind that casting/converting will do something similar to rounding. Other options are to use floor and ceil.
EDIT:
Aaand that was already mentioned above…
there is a fairly easy way to do it if you don’t want to format the value :
mylabel.text = "helloworld: "+floatvalue;
In case you want to format it use something like this:
lblDistanceToPhoto.text = image_distance.ToString(“0.00”);//2dp Number
Thank you, bro!!!
In late! Thanks a lot!!! Very useful!
thz buddy
thanks
Thanks BRO
5 Years later and Thankyou this was helpful, just solved my problem
use Mathf.RoundToInt
Another way to do this is convert it to string and then to an int like so:
float i = 1.2f;
int newInt = Parse(i.ToString("0"));
Use @Dreamora Method, i.e. (int) float_value.
It worked for me.
public float xxx;
public TextMeshProUGUI textxx;
textxx.text = ((int)xxx).ToString();
this show INT .
Casting using (int) is the worst way. It would give wrong values. I casted 0.3/0.1 to Int. It should be 3 but it gives 2.