1.4.22 Numeric Conversion for Calculations

Calculations Sometimes Need Compatible Numeric Types

C# uses several numeric types.

You have already worked with:

int
doubledouble

An int stores whole numbers.

A double can represent values with fractional parts.

When calculations combine different numeric needs, you may need to convert a value to another numeric type.

Integer Division Can Lose the Fractional Part

Consider:

int goals = 7;
int games = 2;

double average = goals / games;

It may look like the result should be:

3.5

But the division:

goals / games

uses two int values.

Integer division produces the whole-number result before it is assigned to the double.

The fractional part is lost.

Convert Before the Calculation When You Need a Fractional Result

You can make one operand a double.

For example:

double average = (double)goals / games;

Now the division is performed using a floating-point numeric type.

Conceptually:

Activity diagram showing goals, then convert to double, then double value games, then fractional result.
Goals.
  • New
  • Changed
  • Removed

This can produce:

3.5
The Cast Changes How the Value Is Used in the Expression

In:

(double)goals

the cast tells C# to treat the current numeric value as a double for the calculation.

It does not permanently change the declaration of:

goals

The original variable can still be declared as:

int goals;
Use Conversion for a Reason

Suppose a soccer application calculates average goals per match.

The data may naturally be stored as whole-number counts:

int totalGoals = 7;
int matchesPlayed = 2;

The average can be fractional.

That makes a floating-point calculation appropriate:

double averageGoals = (double)totalGoals / matchesPlayed;

The conversion supports the meaning of the result.

Assignment Conversion and Calculation Conversion Are Different Questions

Sometimes C# can place a whole-number value into a double variable:

int score = 3;
double numericScore = score;

But that does not fix a calculation that already used integer division.

This:

double average = totalGoals / matchesPlayed;

performs the division first.

If both operands are integers, the fractional portion can already be lost before assignment.

The conversion needs to affect the calculation.

Do Not Convert Everything to double

If a jersey number is:

7

there is usually no reason to represent it as:

7.0

merely because double supports more numeric forms.

Choose types according to the information.

Use conversion when a calculation genuinely needs a different numeric representation.

Numeric Conversion Does Not Change the Real-World Meaning

A player count of:

11

is still eleven players whether C# is currently working with:

11

or:

11.0

The representation changes.

The quantity does not.

Read the Expression in Order

For:

double averageGoals = (double)totalGoals / matchesPlayed;

read:

  1. get totalGoals ;
  2. convert that numeric value to double ;
  3. divide by matchesPlayed ;
  4. produce a fractional-capable result;
  5. store the result in averageGoals .

This makes the purpose of the conversion clear.

Conversion Should Not Be Used to Hide a Bad Type Choice

If a field was designed with the wrong type, repeated conversions may be a symptom of that design problem.

For the current activity, use the types provided by the current design and convert only where the calculation requires it.

The Main Idea

When a calculation needs a fractional result, make sure the expression uses a numeric type capable of representing that result before the division is completed.