Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decimal.ToInt16() Method in C#
The Decimal.ToInt16() method in C# is used to convert the value of the specified Decimal to the equivalent 16-bit signed integer (short). This method performs truncation, discarding any fractional part of the decimal number.
Syntax
Following is the syntax −
public static short ToInt16(decimal val);
Parameters
val − The decimal number to convert.
Return Value
Returns a 16-bit signed integer (short) that is equivalent to the decimal value after truncation. The range of short is -32,768 to 32,767.
Using Decimal.ToInt16() with Positive and Negative Values
The method truncates decimal places and handles both positive and negative values −
using System;
public class Demo {
public static void Main() {
Decimal val1 = -3.578m;
Decimal val2 = 9.352m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
short res1 = Decimal.ToInt16(val1);
short res2 = Decimal.ToInt16(val2);
Console.WriteLine("16-bit signed integer (value1) = " + res1);
Console.WriteLine("16-bit signed integer (value2) = " + res2);
}
}
The output of the above code is −
Decimal 1 = -3.578 Decimal 2 = 9.352 16-bit signed integer (value1) = -3 16-bit signed integer (value2) = 9
Using Decimal.ToInt16() with Small Values
When converting small decimal values, the method truncates towards zero −
using System;
public class Demo {
public static void Main() {
Decimal val1 = 0.001m;
Decimal val2 = 1.000m;
Decimal val3 = 0.999m;
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
short res1 = Decimal.ToInt16(val1);
short res2 = Decimal.ToInt16(val2);
short res3 = Decimal.ToInt16(val3);
Console.WriteLine("16-bit signed integer (value1) = " + res1);
Console.WriteLine("16-bit signed integer (value2) = " + res2);
Console.WriteLine("16-bit signed integer (value3) = " + res3);
}
}
The output of the above code is −
Decimal 1 = 0.001 Decimal 2 = 1.000 Decimal 3 = 0.999 16-bit signed integer (value1) = 0 16-bit signed integer (value2) = 1 16-bit signed integer (value3) = 0
Handling OverflowException
If the decimal value is outside the range of short (-32,768 to 32,767), the method throws an OverflowException −
using System;
public class Demo {
public static void Main() {
Decimal validValue = 32767m;
Decimal invalidValue = 50000m;
try {
short result1 = Decimal.ToInt16(validValue);
Console.WriteLine("Valid conversion: " + validValue + " ? " + result1);
short result2 = Decimal.ToInt16(invalidValue);
Console.WriteLine("This won't execute");
}
catch (OverflowException ex) {
Console.WriteLine("OverflowException: " + ex.Message);
}
}
}
The output of the above code is −
Valid conversion: 32767 ? 32767 OverflowException: Value was either too large or too small for an Int16.
Conclusion
The Decimal.ToInt16() method converts decimal values to 16-bit signed integers by truncating fractional parts. It's important to handle potential OverflowException when working with values outside the short range of -32,768 to 32,767.
