0

I'm new to C#, and I can't work out why I'm getting the following error on these lines of code.

"error CS0266: Cannot implicitly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)"

float rightEdgeOfFormation = (float) transform.position.x + (width * 0.5); float leftEdgeOfFormation = (float) transform.position.x - (width * 0.5); 

I thought writing (float) was the cast?

Thanks so much!

2
  • 2
    I think it's just your brackets. float rightEdgeOfFormation = (float) (transform.position.x + (width * 0.5)); Commented Jul 27, 2017 at 9:25
  • Just remember, when you write somethink like 0.5 it's interpreted as double value, if you want it to be float add f to the end of number like 0.5f. Commented Jul 27, 2017 at 10:27

6 Answers 6

7

A type cast has the highest precedence of all other operations. Therefore, (float) transform.position.x is evaluated before + (width * 0.5). However, (width * 0.5) is a double expression, because the constant 0.5 is a double constant. (You should have used 0.5f if you wanted it to be float.) And when adding a float and a double, C# always "promotes" the float to double. So, the result of float + double is a double, which is then not assignable to a float.

To fix it, either put the entire expression in parentheses before casting to float, or make your 0.5 a float constant by writing it as 0.5f.

Sign up to request clarification or add additional context in comments.

Comments

4

You're multiplying by 0.5, when you're using floats you need to put f at their end.

This will work:

float rightEdgeOfFormation = transform.position.x + (width * 0.5f); float leftEdgeOfFormation = transform.position.x - (width * 0.5f); 

2 Comments

No problem - it happens to everyone, be sure to accept my answer as the correct one :)
Best answer. No need to cast anything, or use the ugly Convert.ToSingle() I've seen in one answer. Always think to use right suffix on your number.
3

Try :

float rightEdgeOfFormation = (float) (transform.position.x + (width * 0.5)); float leftEdgeOfFormation = (float) (transform.position.x - (width * 0.5)); 

you are just casting transform.position.x not the entire expression & something else in the expression is causing the calculation to be done as a double.

Comments

1
float rightEdgeOfFormation = (float)transform.position.x + ((float)width * 0.5F); float leftEdgeOfFormation = (float)transform.position.x - ((float)width * 0.5F); 

See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/float

1 Comment

For me it's not clear that width, transform.position.x and transform.position.y are no doubles. That's the reason why I put a cast in there as well.
1

I think it's just your brackets. float rightEdgeOfFormation = (float) (transform.position.x + (width * 0.5));

Comments

0

Use the following

float rightEdgeOfFormation = Convert.ToSingle(transform.position.x + (width * 0.5)); 

float leftEdgeOfFormation = Convert.ToSingle(transform.position.x - (width * 0.5));

1 Comment

And what do you think about to cast it to a string before to call Convert, just to get sure to have the worse possible solution ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.