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?
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?
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:
MathF on supported runtimes: MathF.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.
Jump to Post— ddanbe 2,724The 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 …
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.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.