0
0
C Sharp (C#)programming~10 mins

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

Choose your learning style9 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#.