- Home /
The question is answered, right answer was accepted
Is there a way to use decimals int Int32.TryParse() ?
I'm trying to make a simple program that takes a player's input and converts it into degree, or turn, things like that. Heres an example of how i've done some of it.
if (angleBoolsOne [1] == true && angleBoolsTwo [5] == true) {
int x;
Int32.TryParse (inputOne.text, out x);
valueTwoText.text = (x / 54).ToString ();
}
If someone put in 1, then the program should return .0185 ... but its returning 0. Is there a way I can keep this close to the same as what I have at but also have decimals?
Thanks
Answer by Eagle-Bound · Dec 05, 2016 at 07:52 PM
What I did was just made it float x
and float.TryParse
Answer by doublemax · Dec 05, 2016 at 02:02 PM
valueTwoText.text = ( (float)x / 54f).ToString ();
Either of those would work as well:
valueTwoText.text = ( x / 54f).ToString ();
valueTwoText.text = ( (float)x / 54).ToString ();
As soon as one of the operands is a float value the result will be a float as well. But when both operands are ints the compiler performs an integer division.
VisualStudio would even mark that float cast as redundant when you use 54f
.