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
Convert.ToInt16 Method in C#
The Convert.ToInt16 method in C# converts a specified value to a 16-bit signed integer (short). This method accepts various data types including double, string, boolean, and other numeric types, and returns a short value ranging from -32,768 to 32,767.
When converting from floating-point numbers, the method performs rounding to the nearest integer. If the value is exactly halfway between two integers, it rounds to the even number.
Syntax
Following are the common overloads of the Convert.ToInt16 method −
public static short ToInt16(double value); public static short ToInt16(string value); public static short ToInt16(bool value); public static short ToInt16(object value);
Parameters
value − The value to convert to a 16-bit signed integer. Can be a number, string, boolean, or object.
Return Value
Returns a 16-bit signed integer (short) equivalent to the specified value. For boolean values, true returns 1 and false returns 0.
Converting from Double
Example
using System;
public class Demo {
public static void Main() {
double doubleNum = 3.456;
short shortNum;
shortNum = Convert.ToInt16(doubleNum);
Console.WriteLine("Converted {0} to {1}", doubleNum, shortNum);
// More examples with rounding
double value1 = 3.5;
double value2 = 4.5;
double value3 = -2.7;
Console.WriteLine("Convert.ToInt16({0}) = {1}", value1, Convert.ToInt16(value1));
Console.WriteLine("Convert.ToInt16({0}) = {1}", value2, Convert.ToInt16(value2));
Console.WriteLine("Convert.ToInt16({0}) = {1}", value3, Convert.ToInt16(value3));
}
}
The output of the above code is −
Converted 3.456 to 3 Convert.ToInt16(3.5) = 4 Convert.ToInt16(4.5) = 4 Convert.ToInt16(-2.7) = -3
Converting from String
Example
using System;
public class Demo {
public static void Main() {
string[] stringValues = { "123", "-456", "0", "32767" };
foreach (string str in stringValues) {
try {
short result = Convert.ToInt16(str);
Console.WriteLine("'{0}' converted to {1}", str, result);
}
catch (Exception ex) {
Console.WriteLine("'{0}' - {1}", str, ex.GetType().Name);
}
}
}
}
The output of the above code is −
'123' converted to 123 '-456' converted to -456 '0' converted to 0 '32767' converted to 32767
Converting from Boolean
Example
using System;
public class Demo {
public static void Main() {
bool trueValue = true;
bool falseValue = false;
short fromTrue = Convert.ToInt16(trueValue);
short fromFalse = Convert.ToInt16(falseValue);
Console.WriteLine("Convert.ToInt16(true) = {0}", fromTrue);
Console.WriteLine("Convert.ToInt16(false) = {0}", fromFalse);
}
}
The output of the above code is −
Convert.ToInt16(true) = 1 Convert.ToInt16(false) = 0
Common Conversion Types
| Source Type | Conversion Behavior | Example |
|---|---|---|
| double/float | Rounds to nearest integer | 3.7 ? 4 |
| string | Parses numeric string | "123" ? 123 |
| bool | true = 1, false = 0 | true ? 1 |
| int/long | Direct conversion (if in range) | 1000 ? 1000 |
Conclusion
The Convert.ToInt16 method provides a reliable way to convert various data types to 16-bit signed integers. It handles rounding for floating-point numbers and can convert strings, booleans, and other numeric types, making it essential for type conversions in C# applications.
