What is the Decimal.ToInt32() method in C#?

Overview

The Decimal.ToInt32() method in C# converts the value of the specified decimal to the equivalent 32-bit signed integer.

Note: If the whole number part of the decimal value is greater than a 32-bit signed integer, an error will be thrown upon conversion.

Int32 values range from -2,147,483,648 to 2,147,483,647.

Syntax

public static int ToInt32 (decimal d);

Parameters

d: This is the decimal value that we want to convert to the equivalent 32-bit signed integer.

Return value

An Int32 value is returned. It is a 32-bit signed integer equivalent of the specified decimal value.

Code example

// use System
using System;
// create class
class DecimalToInt32
{
// main method
static void Main()
{
// create decimal values
decimal d1 = 323.45m;
decimal d2 = 1.88455435m;
decimal d3 = -0.34m;
decimal d4 = 12.88888m;
// convert to Int32
// and print results
Console.WriteLine(Decimal.ToInt32(d1));
Console.WriteLine(Decimal.ToInt32(d2));
Console.WriteLine(Decimal.ToInt32(d3));
Console.WriteLine(Decimal.ToInt32(d4));
}
}

Explanation

  • Lines 11-14: We create variables d1, d2, d3, and d4 of type decimal and assign decimal values to them.

  • Lines 18-21: We convert each value to a signed 32-bit integer using the Decimal.ToInt32() method and print the results.

Free Resources