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?

Dani AI

Generated

A concise, practical add-on to what asked and explained: here are the real-world tradeoffs and gotchas to keep in mind when choosing and using float (System.Single) in C#.

float is a 32‑bit IEEE‑754 single‑precision type with roughly 7 significant decimal digits and a limited exponent range (about ±1.5e‑45 to ±3.4e38). double (64‑bit) offers much more precision (~15–16 digits). Use float when memory or throughput matters (graphics, large numeric buffers, some simulations); use double for general numeric work; use decimal for monetary or exact-decimal needs. See Microsoft’s summary of floating-point types for exact specs: Floating-point numeric types.

Practical tips and pitfalls:

  • Floating math rounds. Avoid testing floats for exact equality; compare with a tolerance that scales with magnitude (e.g., max(relTol * max(|a|,|b|), absTol)). Microsoft documents recommended comparison patterns here: .
  • Prefer single-precision math APIs when targeting floats to avoid needless casts and extra rounding—use MathF on supported runtimes: MathF.
  • Remember the standard constants (float.MaxValue, float.MinValue, float.Epsilon) and choose formatting that preserves precision (ToString("R") or appropriate "G" precision).

These points complement the literal-syntax discussion in this thread by focusing on when floats are appropriate and how to use them safely in real code.

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.