How to Store a Number with a Fraction in a float or double in C#
double height = 1.75; and float weight = 68.5f; both store a number with a fraction. A double keeps about twice as many digits as a float, and a float literal needs an f at the end.
The C# reference: The floating-point numeric types represent real numbers. Real numbers include the ones with a fraction, such as 1.75. double and float are two of these types, and the reference's table sets them apart:
| C# type | Approximate range | Precision | Size | .NET type |
|---|---|---|---|---|
float | ±1.5 x 10^−45 to ±3.4 x 10^38 | ~6-9 digits | 4 bytes | System.Single |
double | ±5.0 × 10^−324 to ±1.7 × 10^308 | ~15-17 digits | 8 bytes | System.Double |
The reference adds: In the preceding table, each C# type keyword from the leftmost column is an alias for the corresponding .NET type. They're interchangeable.
Syntax
double <name> = <whole>.<fraction>;
float <name> = <whole>.<fraction>f;
| Part | What it is |
|---|---|
double or float | The type of the variable |
<name> | The variable's name |
<whole>.<fraction> | The number, with a full stop before the fraction, such as 1.75 |
f | The suffix that makes the literal a float. F means the same |
The suffix decides the literal's type. The reference: A literal without a suffix or with the d or D suffix is a double. And: A literal with the f or F suffix is a float.
The Example
A console app called FloatDouble stores a height in a double and a weight in a float, works out one third in each type, and prints each type's size and largest value.
Prerequisites
- The .NET 10 SDK.
dotnet --versionprints a version that starts with10.0.
Step 1: Create a Console App
dotnet new console -n FloatDouble
cd FloatDouble
The template "Console App" was created successfully.
Processing post-creation actions...
Restoring /Users/you/source/FloatDouble/FloatDouble.csproj:
Determining projects to restore...
Restored /Users/you/source/FloatDouble/FloatDouble.csproj (in 44 ms).
Restore succeeded.
dotnet new console -n FloatDouble creates a folder named FloatDouble with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or FloatDouble.csproj in Visual Studio.
Step 2: Write the Code
Replace everything in Program.cs. First the culture and the two variables:
using System.Globalization;
// Print numbers the same way on every computer.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
// A double needs no suffix. A float needs an f.
double height = 1.75;
float weight = 68.5f;
Console.WriteLine($"Height: {height} m, weight: {weight} kg");
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;makes the program print a full stop before the decimals on every computer.double height = 1.75;needs no suffix, because1.75is adoublealready.float weight = 68.5f;stores 68.5 as afloat.
Then one third in each type:
// One third, as each type stores it.
double thirdAsDouble = 1.0 / 3;
float thirdAsFloat = 1.0f / 3;
Console.WriteLine($"1/3 as a double: {thirdAsDouble}");
Console.WriteLine($"1/3 as a float: {thirdAsFloat}");
1.0 / 3 divides a double, so the result is a double. 1.0f / 3 divides a float, so the result is a float.
Then the sizes and the largest values:
// Their sizes and their largest values.
Console.WriteLine($"double: {sizeof(double)} bytes, up to {double.MaxValue}");
Console.WriteLine($"float: {sizeof(float)} bytes, up to {float.MaxValue}");
sizeof(double) gives the number of bytes a double takes, and double.MaxValue is the largest number a double can hold.
Step 3: Run It
Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:
dotnet run
Height: 1.75 m, weight: 68.5 kg
1/3 as a double: 0.3333333333333333
1/3 as a float: 0.33333334
double: 8 bytes, up to 1.7976931348623157E+308
float: 4 bytes, up to 3.4028235E+38

- 1.75 and 68.5 printed as written.
- One third came out as 0.3333333333333333 in the
double, with 16 threes, and as 0.33333334 in thefloat, which keeps fewer digits and rounded the last one. - A
doubletook 8 bytes and goes up to 1.7976931348623157E+308, and afloattook 4 bytes and goes up to 3.4028235E+38.E+308means "times 10 to the power of 308".
The Complete File

using System.Globalization;
// Print numbers the same way on every computer.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
// A double needs no suffix. A float needs an f.
double height = 1.75;
float weight = 68.5f;
Console.WriteLine($"Height: {height} m, weight: {weight} kg");
// One third, as each type stores it.
double thirdAsDouble = 1.0 / 3;
float thirdAsFloat = 1.0f / 3;
Console.WriteLine($"1/3 as a double: {thirdAsDouble}");
Console.WriteLine($"1/3 as a float: {thirdAsFloat}");
// Their sizes and their largest values.
Console.WriteLine($"double: {sizeof(double)} bytes, up to {double.MaxValue}");
Console.WriteLine($"float: {sizeof(float)} bytes, up to {float.MaxValue}");
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving the f off a float literal, as in float weight = 68.5; | error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type | Write 68.5f |
Putting a double into a float, as in float shorter = height; | error CS0266: Cannot implicitly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?) The reference: There's only one implicit conversion between floating-point numeric types: from float to double. | Cast on purpose: (float)height gave 1.75. The other way, double wider = weight; needs no cast and gave 68.5 |
Comparing results with == | 3 * 0.1 was 0.30000000000000004, so 3 * 0.1 == 0.3 was False. The reference: Because some numbers can't be represented exactly as fractional binary values, floating-point numbers can only approximate real numbers. | Check that the difference is tiny: Math.Abs(3 * 0.1 - 0.3) < 0.000001 was True |
Keeping a long whole number in a float | 123456789f printed 123456790, and converted to a whole number it was 123456792: only the first seven digits survived | Use a double, which printed 123456789 |
FAQ
Should I use double or float?
Use double unless memory matters: a literal with a fraction is a double already, and it keeps about twice as many digits. A float takes half the space, which adds up when you store millions of numbers. The reference's example: ML.NET uses float because the difference between 4 bytes and 16 bytes adds up for very large data sets.
What about money?
Use decimal, not float or double. The reference: 0.1, for example, can be exactly represented by a decimal instance, while there's no double or float instance that exactly represents 0.1.
Conclusion
Store numbers with a fraction in a double, and in a float with an f suffix when you need to save memory and 6 to 9 digits are enough. Neither type holds every decimal fraction exactly, so compare their results with a tolerance, not ==.
Additional Resources
- Floating-point numeric types — float, double and decimal, their ranges and their literals.
- System.Double — precision, and testing two values for equality.
- System.Single — the .NET type behind float.
