Skip to main content

How to Store a Number with a Fraction in a float or double in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

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# typeApproximate rangePrecisionSize.NET type
float±1.5 x 10^−45 to ±3.4 x 10^38~6-9 digits4 bytesSystem.Single
double±5.0 × 10^−324 to ±1.7 × 10^308~15-17 digits8 bytesSystem.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​

Syntax
double <name> = <whole>.<fraction>;
float <name> = <whole>.<fraction>f;
PartWhat it is
double or floatThe type of the variable
<name>The variable's name
<whole>.<fraction>The number, with a full stop before the fraction, such as 1.75
fThe 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 --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n FloatDouble
cd FloatDouble
Output
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:

Program.cs (the 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, because 1.75 is a double already.
  • float weight = 68.5f; stores 68.5 as a float.

Then one third in each type:

Program.cs (one third)
// 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:

Program.cs (sizes and 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:

Terminal
dotnet run
Output
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

macOS-style terminal window titled Terminal — FloatDouble showing dotnet run printing Height: 1.75 m, weight: 68.5 kg, one third as 0.3333333333333333 for the double and 0.33333334 for the float, double: 8 bytes, up to 1.7976931348623157E+308, and 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 the float, which keeps fewer digits and rounded the last one.
  • A double took 8 bytes and goes up to 1.7976931348623157E+308, and a float took 4 bytes and goes up to 3.4028235E+38. E+308 means "times 10 to the power of 308".

The Complete File​

macOS-style code window titled Program.cs — FloatDouble showing the invariant culture, a double height of 1.75 and a float weight of 68.5f, one third worked out as a double and as a float, and the size and largest value of each type

Program.cs
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​

MistakeWhat happensDo 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 typeWrite 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 float123456789f printed 123456790, and converted to a whole number it was 123456792: only the first seven digits survivedUse 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​