Skip to main content

How to Mix int and decimal Values in One Calculation in C#

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

hours * hourlyRate, an int times a decimal, needs no conversion code: C# turns the int into a decimal first, and the result is a decimal. Going back from decimal to int is the step that needs a cast.

The C# reference: C# provides a set of integral and floating-point numeric types. An implicit or explicit conversion exists between any two numeric types. Use a cast expression to perform an explicit conversion. An implicit conversion happens by itself, and an explicit one is a cast you write, such as (int).

In a calculation with two different types, the reference says: If the operands are different integral or floating-point types, their values are converted to the closest containing type, if such a type exists.

Syntax​

Syntax
<whole> <operator> <decimal or double>
(<type>)<value>
PartWhat it is
<whole>A whole number of type int, such as hours or 500
<operator>An arithmetic or comparison operator, such as *, / or >=
<decimal or double>A number of type decimal or double. The int is converted to its type before the operator runs
(<type>)<value>A cast, such as (int)pay: the explicit conversion you need to go from decimal or double to int

The rules, in the reference's words: Any integral numeric type is implicitly convertible to any floating-point numeric type. And for the result: You can also mix integral types and the decimal type in an expression. In this case, integral types are implicitly converted to the decimal type and the expression evaluates to decimal, or to bool in relational and equality comparisons.

The Example​

A console app called IntDecimal works out a week's pay from 38 hours at 15.75 an hour, turns 90 minutes into hours, compares the pay with 500, and casts the pay back to an int.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n IntDecimal
cd IntDecimal
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/IntDecimal/IntDecimal.csproj:
Determining projects to restore...
Restored /Users/you/source/IntDecimal/IntDecimal.csproj (in 46 ms).
Restore succeeded.

dotnet new console -n IntDecimal creates a folder named IntDecimal with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or IntDecimal.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the culture and the pay:

Program.cs (int times decimal)
using System.Globalization;

// Print numbers the same way on every computer.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

// An int times a decimal: the int becomes a decimal first.
int hours = 38;
decimal hourlyRate = 15.75m;
var pay = hours * hourlyRate;
Console.WriteLine($"{hours} * {hourlyRate} = {pay} ({pay.GetType().Name})");
  • CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; makes the program print a full stop before decimals on every computer.
  • var pay = hours * hourlyRate; lets C# choose the type of pay from the calculation, and pay.GetType().Name prints which type that is.

Then a double and a comparison:

Program.cs (a double and a comparison)
// An int divided by a double: the int becomes a double.
int minutes = 90;
var inHours = minutes / 60.0;
Console.WriteLine($"{minutes} / 60.0 = {inHours} ({inHours.GetType().Name})");

// An int in a comparison with a decimal works the same way.
Console.WriteLine($"{pay} >= 500 is {pay >= 500}");
  • minutes / 60.0 divides an int by the double literal 60.0.
  • pay >= 500 compares the decimal pay with the int 500.

Then the cast:

Program.cs (the cast)
// Back to an int takes a cast, which drops the fraction.
int wholePay = (int)pay;
Console.WriteLine($"(int){pay} = {wholePay}");

(int)pay is an explicit conversion: you have to ask for it, because the fraction can be lost.

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
38 * 15.75 = 598.50 (Decimal)
90 / 60.0 = 1.5 (Double)
598.50 >= 500 is True
(int)598.50 = 598

macOS-style terminal window titled Terminal — IntDecimal showing dotnet run printing 38 times 15.75 = 598.50 (Decimal), 90 / 60.0 = 1.5 (Double), 598.50 >= 500 is True and (int)598.50 = 598

  • 38 * 15.75 gave 598.50, a Decimal: the int 38 became a decimal before the multiplication. Decimal and Double are the .NET names that GetType().Name prints for decimal and double.
  • 90 / 60.0 gave 1.5, a Double.
  • 598.50 >= 500 was True: the int 500 became a decimal for the comparison.
  • (int)598.50 gave 598: the cast dropped the fraction. The reference: When you convert a decimal value to an integral type, this value is rounded toward zero to the nearest integral value.

The Complete File​

macOS-style code window titled Program.cs — IntDecimal showing the invariant culture, 38 hours times a decimal rate of 15.75m stored with var and printed with its type, 90 minutes divided by 60.0, a comparison of the pay with 500, and a cast of the pay to int

Program.cs
using System.Globalization;

// Print numbers the same way on every computer.
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;

// An int times a decimal: the int becomes a decimal first.
int hours = 38;
decimal hourlyRate = 15.75m;
var pay = hours * hourlyRate;
Console.WriteLine($"{hours} * {hourlyRate} = {pay} ({pay.GetType().Name})");

// An int divided by a double: the int becomes a double.
int minutes = 90;
var inHours = minutes / 60.0;
Console.WriteLine($"{minutes} / 60.0 = {inHours} ({inHours.GetType().Name})");

// An int in a comparison with a decimal works the same way.
Console.WriteLine($"{pay} >= 500 is {pay >= 500}");

// Back to an int takes a cast, which drops the fraction.
int wholePay = (int)pay;
Console.WriteLine($"(int){pay} = {wholePay}");

Common Mistakes​

MistakeWhat happensDo this instead
Putting the result in an int, as in int pay = hours * hourlyRate;error CS0266: Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?) The reference: There are no implicit conversions from the double and decimal types.Store it in a decimal, or cast on purpose with (int)
Mixing a decimal and a double, as in hourlyRate * 1.5error CS0019: Operator '*' cannot be applied to operands of type 'decimal' and 'double' The reference: There are no implicit conversions between the decimal type and the float or double types.Write the literal as a decimal: hourlyRate * 1.5m gave 23.625
Dividing two int values into a decimal, as in decimal perDay = hours / 4;It gave 9, not 9.5: 38 / 4 ran as int division, and only its result became a decimalMake one side a decimal: hours / 4m gave 9.5

FAQ​

Which conversions happen by themselves?​

The ones in the reference's table of implicit conversions. For int, it lists long, float, double, decimal, or nint. An int went into a long, a double and a decimal without a cast, and all three held 38. The other way, int whole = inHours; with a double gave error CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

Conclusion​

In a calculation, an int adapts to a decimal or a double: it's converted to that type by itself, and the result has that type. Going back to int takes a cast, which drops the fraction, and a decimal and a double never mix without one.

Additional Resources​