1

I'M reading "Head First C#" In this book I was told that when you want to create a float that you need to add an "F" to the end.

float myFloat = 14.6F;

But the very first thing you type is "float", isn't that enough?

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by ddanbe
0

The reason is perhaps strangely enough quite simple!
C# interprets literals like 234.123 as a double and not as a float.
You cannot assign a double to a float directly.
So if you explicitly want to say that 234.123 is a float, you have to append an f or F to it.
The Math class also, only works with double types.

0

So even if I declare a variable preceded with the keyword "float", it will still be read as double if I don't tack an "F/f" to the end?

The reason is perhaps strangely enough quite simple!
C# interprets literals like 234.123 as a double and not as a float.
You cannot assign a double to a float directly.
So if you explicitly want to say that 234.123 is a float, you have to append an f or F to it.
The Math class also, only works with double types.

0

Yes, I know this can be annoying. I believe the type only exists for backward compatibility with C, C++ or other libraries.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.