Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Floating point types (float, double, decimal) in C Sharp (C#) - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Floating point types (float, double, decimal)
Declare variable with type
↓
Assign value (literal)
↓
Store value in memory
↓
Use variable in calculations
↓
Observe precision and range
↓
Output or further use
This flow shows how floating point variables are declared, assigned values, stored, used in calculations, and how their precision and range affect results.
Execution Sample
C Sharp (C#)
float f = 1.23456789f;
double d = 1.23456789012345;
decimal m = 1.2345678901234567890123456789m;
Console.WriteLine(f);
Console.WriteLine(d);
Console.WriteLine(m);
This code declares three floating point variables with different types and prints their values to show differences in precision.
Execution Table
StepActionVariableValue AssignedPrecision/NotesOutput
1Declare and assign floatf1.23456789fApprox. 7 digits precision
2Declare and assign doubled1.23456789012345Approx. 15-16 digits precision
3Declare and assign decimalm1.2345678901234567890123456789mApprox. 28-29 digits precision
4Print float valuef1.234568Rounded to 7 digits1.234568
5Print double valued1.23456789012345Shows more digits1.23456789012345
6Print decimal valuem1.2345678901234567890123456789Full precision shown1.2345678901234567890123456789
7End of program
💡 Program ends after printing all three values showing their precision differences.
Variable Tracker
VariableStartAfter AssignmentFinal
fundefined1.234568 (approx)1.234568 (stored approx)
dundefined1.234567890123451.23456789012345
mundefined1.23456789012345678901234567891.2345678901234567890123456789
Key Moments - 3 Insights
Why does the float variable show fewer digits than the decimal when printed?
Float has about 7 digits of precision, so it rounds the value when stored and printed, as seen in execution_table rows 1 and 4.
Why do we add 'f', 'd', or 'm' after the number when assigning?
These suffixes tell the compiler which floating point type to use. Without them, the number defaults to double (for floating-point literals without suffix) or decimal (with 'm'), as shown in execution_table rows 1-3.
Can decimal store more precise values than double?
Yes, decimal stores about 28-29 digits of precision, more than double's 15-16 digits, which is why the decimal value prints fully in row 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the printed value of the float variable 'f'?
A1.234568
B1.23456789
C1.23456789012345
D1.2345678901234567890123456789
💡 Hint
Check the 'Output' column at step 4 in the execution_table.
At which step does the program assign the variable with the highest precision?
AStep 1 (float)
BStep 2 (double)
CStep 3 (decimal)
DStep 4 (print float)
💡 Hint
Look at the 'Precision/Notes' column in the execution_table for assignment steps.
If we remove the 'f' suffix from the float assignment, what will happen?
AThe float will store the value correctly without suffix.
BThe code will not compile because the literal is treated as double by default.
CThe decimal type will be assigned instead.
DThe program will print the same value.
💡 Hint
Refer to key_moments about suffixes and their role in type assignment.
Concept Snapshot
Floating point types in C#:
- float: 32-bit, ~7 digits precision, suffix 'f'
- double: 64-bit, ~15-16 digits precision, default for floating-point literals without suffix
- decimal: 128-bit, ~28-29 digits precision, suffix 'm'
Use suffixes to specify type explicitly.
Precision affects stored and printed values.
Full Transcript
This visual execution shows how floating point types float, double, and decimal work in C#. Variables are declared with their types and assigned values with suffixes to specify type. Float stores about 7 digits precision, double about 15-16, and decimal about 28-29 digits. When printed, float rounds the value, double shows more digits, and decimal shows full precision. Suffixes like 'f' for float and 'm' for decimal are necessary to tell the compiler the intended type. Removing suffixes can cause compilation errors or unintended types. This helps understand how precision and type affect floating point numbers in C#.

Practice

(1/5)
1.

Which C# type is best to store money values accurately?

easy
A. float
B. int
C. double
D. decimal

Solution

  1. Step 1: Understand precision needs for money

    Money values require high precision and no rounding errors.
  2. Step 2: Compare types for precision

    decimal type provides high precision and is designed for financial calculations.
  3. Final Answer:

    decimal -> Option D
  4. Quick Check:

    Money uses decimal for accuracy [OK]
Hint: Use decimal for money to avoid rounding errors [OK]
Common Mistakes:
  • Choosing float or double for money causes precision loss
  • Using int for decimal values
  • Forgetting to add 'm' suffix for decimal literals
2.

Which of the following is the correct way to declare a float variable with value 3.14?

?
easy
A. float pi = 3.14m;
B. float pi = 3.14;
C. float pi = 3.14f;
D. float pi = 3.14d;

Solution

  1. Step 1: Recall float literal syntax

    Float literals require an 'f' suffix to specify the type explicitly.
  2. Step 2: Check each option

    float pi = 3.14f; uses 3.14f which is correct. float pi = 3.14; lacks suffix, so treated as double. float pi = 3.14m; uses 'm' for decimal, wrong type. float pi = 3.14d; assigns double literal to float, compilation error.
  3. Final Answer:

    float pi = 3.14f; -> Option C
  4. Quick Check:

    Float literals need 'f' suffix [OK]
Hint: Add 'f' suffix for float literals [OK]
Common Mistakes:
  • Omitting 'f' suffix causes errors
  • Using 'm' suffix for float
  • Assigning double literal directly to float
3.

What is the output of this C# code?

float a = 0.1f;
double b = 0.1;
Console.WriteLine(a == b);
medium
A. False
B. Compilation error
C. True
D. Runtime exception

Solution

  1. Step 1: Understand operator restrictions

    C# allows comparison between float and double by promoting float to double.
  2. Step 2: Evaluate the comparison

    float a = 0.1f is less precise than double b = 0.1, so when promoted to double, a != b due to precision differences.
  3. Final Answer:

    False -> Option A
  4. Quick Check:

    float 0.1f != double 0.1 due to precision [OK]
Hint: Comparing float and double can yield false due to precision differences [OK]
Common Mistakes:
  • Assuming float equals double for same number
  • Thinking it causes compilation error
  • Expecting runtime exception
4.

Identify the error in this code snippet:

decimal price = 19.99m;
float discount = 0.1f;
decimal finalPrice = price - discount;
medium
A. Cannot subtract float from decimal directly
B. Cannot assign double literal to decimal without suffix
C. Missing 'f' suffix on float literal
D. No error, code is correct

Solution

  1. Step 1: Check literal suffixes

    decimal uses 19.99m (correct 'm' suffix), float uses 0.1f (correct).
  2. Step 2: Check operation between decimal and float

    Subtracting float from decimal is invalid without explicit conversion.
  3. Final Answer:

    Cannot subtract float from decimal directly -> Option A
  4. Quick Check:

    decimal and float arithmetic needs explicit casts [OK]
Hint: decimal and float can't mix in math without cast [OK]
Common Mistakes:
  • Ignoring type mismatch in arithmetic
  • Forgetting 'm' suffix for decimal literals
  • Assuming implicit conversion between float and decimal
5.

You want to store the value 123.4567890123456789 with the highest precision possible in C#. Which type should you choose and how should you write the literal?

hard
A. Use float and write 123.4567890123456789f
B. Use decimal and write 123.4567890123456789m
C. Use double and write 123.4567890123456789d
D. Use double and write 123.4567890123456789

Solution

  1. Step 1: Understand precision limits of types

    float has ~7 digits precision, double ~15-17 digits, decimal ~28-29 digits.
  2. Step 2: Choose type and literal suffix

    decimal provides highest precision and requires 'm' suffix for literals.
  3. Final Answer:

    Use decimal and write 123.4567890123456789m -> Option B
  4. Quick Check:

    decimal + 'm' suffix = highest precision [OK]
Hint: decimal with 'm' suffix stores highest precision decimals [OK]
Common Mistakes:
  • Using float or double for high precision
  • Omitting 'm' suffix for decimal literals
  • Assuming double is more precise than decimal
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard