- Home /
How to round or convert doubles?
Hey all! I am having an issue with doubles. I need a way to either round doubles to the nearest integer, or to convert them to a float or an int. Specifically, I need to get doubles to work with Mathf.RoundtoInt(). Thanks in advance!
Answer by robertbu · Jan 21, 2014 at 06:40 AM
In C# you can just cast them:
int myInt = Mathf.RoundToInt((float)myDouble);
Or, even (int)((myDouble+0.5)
, for non-negative numbers.
Answer by getyour411 · Jan 21, 2014 at 06:37 AM
I'm going to assume you are just going off an error in compiler complaining about doubles where you use '6.5'; if so, put an "f" at the end, like 6.5f
If you are really looking for more double/round stuff, let me know.
Thanks for your quick answer. How would I go about doing this for a variable that is intentionally a double? $$anonymous$$onoDevelop recognizes it as an undefined variable rather than a float.
As @robertbu suggests; does that answer this question?
yes casting would do that. @robertbu you can convert that to an answer
Your answer
Follow this Question
Related Questions
Periodic fraction gets converted to int 1 Answer
Is it possible to round down 0.99 to 0? 2 Answers
Rounding to the nearest int as a Result of a Function? 1 Answer
Float to Int 4 Answers
PlayerPrefs Int to Float 1 Answer