Skip to content

Numbers and Text

The Course
•
25 min study

C# has the usual arithmetic operators: +, -, * and /, plus %, which gives the remainder of a division. Multiplication and division happen before addition and subtraction, and parentheses make the order explicit. The surprise is in what / does with two whole numbers.

Dividing whole numbers

When both sides of / are int, the result is an int as well, and any fractional part is thrown away. It isn't rounded. If at least one side is a double, you get a double. % is the natural partner of whole-number division, because it gives you what was thrown away.

Predict all four results C#
Console.WriteLine(95 / 60);
Console.WriteLine(95 % 60);
Console.WriteLine(95 / 60.0);
Console.WriteLine(2 + 3 * 4);
Check your prediction
Text
1
35
1.5833333333333333
14

95 minutes is 1 hour and 35 minutes, which is exactly what the first two lines say. When your values are in int variables you can't add .0, so you cast one side, by writing the type you want in parentheses before it. The cast must happen before the division. Otherwise the fraction has already been lost.

Cast before you divide C#
int total = 95;
int sessions = 2;

Console.WriteLine(total / sessions);
Console.WriteLine((double)total / sessions);
Console.WriteLine((double)(total / sessions));
Check your prediction
Text
47
47.5
47

Building text with interpolation

Put $ before the opening quote and you can place any expression inside braces in the text. This is called string interpolation, and it's the usual way to build output in C#. After the expression, a colon can add a format: F1 means one digit after the decimal point.

Holes in the text C#
string topic = "Loops";
int minutes = 95;
double hours = minutes / 60.0;

Console.WriteLine($"{topic}: {minutes} minutes");
Console.WriteLine($"That is {minutes / 60}h {minutes % 60}m");
Console.WriteLine($"Or about {hours:F1} hours");
Check your prediction
Text
Loops: 95 minutes
That is 1h 35m
Or about 1.6 hours

Without the $, the braces are just ordinary characters and you'd see {topic} printed literally. Remove the $ from one line to check.

You can also join strings with +, and numbers are converted to text along the way. It reads worse once there are several pieces, and it has a trap: + works left to right, so "Total: " + 1 + 2 gives Total: 12, while 1 + 2 + " total" gives 3 total.

Money needs decimal

double stores numbers in binary, so some decimal fractions can only be stored approximately. That's fine for averages but wrong for money. C# has a third number type, decimal, which stores decimal digits exactly. Write its values with an m suffix.

double and decimal C#
Console.WriteLine(0.1 + 0.2);
Console.WriteLine(0.1m + 0.2m);

decimal price = 12.50m;
Console.WriteLine($"Three books: ${price * 3:F2}");
Check your prediction
Text
0.30000000000000004
0.3
Three books: $37.50

The $ inside the text is just a dollar sign. Only the one before the opening quote is special.

Your turn

Exercise

Average a study week

A learner studied 40, 25 and 30 minutes in three sessions. Store those values and print the total, then the average with one digit after the decimal point, in exactly this form: Total: 95 minutes and Average: 31.7 minutes.

Your workspace C#
int first = 40;
int second = 25;
int third = 30;
Console.WriteLine(first);
Hint 1: the idea

The average needs a fractional part, so the division must not be a whole-number division.

Hint 2: a first step

Add the three values into a new int called total, and print that before you work on the average.

Show one solution
C#
int first = 40;
int second = 25;
int third = 30;

int total = first + second + third;
double average = (double)total / 3;

Console.WriteLine($"Total: {total} minutes");
Console.WriteLine($"Average: {average:F1} minutes");
Text
Total: 95 minutes
Average: 31.7 minutes

(double)total / 3 converts before dividing. Without the cast, 95 / 3 is whole-number division and gives 31, losing the .7. :F1 only rounds the display to one decimal place; the value itself keeps all its digits.

Remove the (double) cast and predict the new average before you run it. Explain why it prints 31.0 and not 31.7.

Was this article helpful?

Your response is saved on this device.