- Home /
The question is answered, right answer was accepted
Convert double to float for Vector3
I get a "Cannot implicitly convert 'double' to 'float'. An explicit conversion exists (are you missing a cast?)" message for
the following formula to be used as argument in a Vector3:
float sph_x = Input.mousePosition.x/1280*7.7-3.85f; //sph_x would be 0,22859374999999900000
Surprisingly it works for this formula : float sph_x = Input.mousePosition.x/1280*7-3.85f; //sph_x would be -0,14218750000000000000
I need only 2 or 3 decimals of precision. Is there no conversion from double to float? I found only the Mathf.RoundToInt.
Not even if I multiply by 1000, Round to int and then divide back by 1000 doesn't get a float:
float sph_y = ( Mathf.RoundToInt (1000* (Input.mousePosition.x/1280*7.65-3.85f) ) )/1000;
Now it gets 2 errors: the "cannot implicitly ..." and another one "best overloaded method..." Dividing an integer by 1000 results in 3 decimals ONLY so a float should be OK.
Only it isn't.
There has to be a nice simple way to do this...
Thanks.
Thanks, Owen and Paulius. What can I say? There really should be a delete button for this question
Answer by Paulius-Liekis · Dec 24, 2014 at 12:52 PM
7.7
needs an f in the end.
Otherwise you can always do this, i.e. add explicit cast just as error tells you too:
float sph_x = (float)(Input.mousePosition.x/1280*7.7-3.85f);
Follow this Question
Related Questions
Vector3 using Doubles Instead of Floats 2 Answers
C# CS0266 error for multiplying by a decimal 1 Answer
Convert float in Transform to double 1 Answer