📖 Scenario: You are creating a simple program to store prices of items in a store. Different types of floating point numbers are used to understand how they behave with money values.
🎯 Goal: Build a C# program that declares variables using float, double, and decimal types, assigns values, calculates a total price, and prints the result.
📋 What You'll Learn
Create three variables named priceFloat, priceDouble, and priceDecimal with types float, double, and decimal respectively.
Assign the value 19.99 to each variable using the correct suffix for float and decimal types.
Create a variable called quantity of type int and assign it the value 3.
Calculate the total price for each type by multiplying the price by the quantity.
Print the total prices with clear labels.
💡 Why This Matters
🌍 Real World
Floating point types are used in programs that handle measurements, prices, and scientific calculations where decimals are needed.
💼 Career
Understanding float, double, and decimal types is important for software developers working with financial applications, games, simulations, and data analysis.
Progress0 / 4 steps
1
Declare floating point variables
Create three variables: priceFloat of type float with value 19.99f, priceDouble of type double with value 19.99, and priceDecimal of type decimal with value 19.99m.
C Sharp (C#)
Hint
Remember to add f after the number for float and m for decimal values.
2
Declare quantity variable
Create an integer variable called quantity and assign it the value 3.
C Sharp (C#)
Hint
Use int type for whole numbers like quantity.
3
Calculate total prices
Create three variables: totalFloat, totalDouble, and totalDecimal. Calculate each by multiplying the corresponding price variable by quantity.
C Sharp (C#)
Hint
Multiply price by quantity for each type and store in a new variable.
4
Print total prices
Print the values of totalFloat, totalDouble, and totalDecimal with labels: "Total price (float):", "Total price (double):", and "Total price (decimal):" respectively.
C Sharp (C#)
Hint
Use Console.WriteLine to print each total with the correct label.
Practice
(1/5)
1.
Which C# type is best to store money values accurately?
easy
A. float
B. int
C. double
D. decimal
Solution
Step 1: Understand precision needs for money
Money values require high precision and no rounding errors.
Step 2: Compare types for precision
decimal type provides high precision and is designed for financial calculations.
Final Answer:
decimal -> Option D
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
Step 1: Recall float literal syntax
Float literals require an 'f' suffix to specify the type explicitly.
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.
Final Answer:
float pi = 3.14f; -> Option C
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
Step 1: Understand operator restrictions
C# allows comparison between float and double by promoting float to double.
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.
Final Answer:
False -> Option A
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]
Subtracting float from decimal is invalid without explicit conversion.
Final Answer:
Cannot subtract float from decimal directly -> Option A
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?