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?

Recommended Answers

All 3 Replies

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.