- Home /
C# CS0266 error for multiplying by a decimal
I'm not sure why the function below yields this error. It seems to happen because of the *0.5 ... Is it not possible to multiply the sum by a decimal?
error CS0266: Cannot implicitly convert type double' to
float'. An explicit conversion exists (are you missing a cast?)
/*
* Function: sliderPercentage
* returns percentage on transform *t* that we hit with point *p*
*/
float sliderPercentage(Transform t,Vector3 p,Vector3 dir){
return (Vector3.Dot(dir,p) - Vector3.Dot(dir,t.localScale)*0.5 + Vector3.Dot(t.position, dir)) / Vector3.Dot(dir,t.localScale);
}
You forgot the f after 0.5. Not sure if that solves the problem though. :)
It seems your return is a double while the function returns float. Try with
return (float)(Vector3.Dot(dir,p) - Vector3.Dot(dir,t.localScale)*0.5 + Vector3.Dot(t.position, dir)) / Vector3.Dot(dir,t.localScale);
you don't really need the f. Putting the .f just tells the compiler that you want a float, not mentioning, the compiler casts to the most appropriate type.
Right, thanks, slowly learning C# by answering questions wrong and getting corrected ... :D I'm a JS programmer at heart!
Answer by Paulius-Liekis · Apr 09, 2012 at 02:55 PM
Change:
to 0.5f or
return type from float to double
Is it always necessary to append an f to declare a decimal a float?
C# compiler see any decimal number as a double unless you specify its a float. So yes you need either the f or a (float) casting. Integers will be correctly interpreted as so, as long as you keep it to 1 and not 1.0, for example.
Your answer
![](https://koobas.hobune.stream/wayback/20220613070103im_/https://answers.unity.com/themes/thub/images/avi.jpg)