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
C# Int16.ToString() Method
The Int16.ToString() method in C# is used to convert a 16-bit signed integer (short) to its equivalent string representation. This method provides multiple overloads to format the output string according to your specific requirements.
Syntax
Following are the different overloads of the Int16.ToString() method −
public override string ToString(); public string ToString(string format); public string ToString(IFormatProvider provider); public string ToString(string format, IFormatProvider provider);
Parameters
-
format − A numeric format string that specifies the format of the return value.
-
provider − An object that supplies culture-specific formatting information.
Return Value
Returns a string representation of the value of this instance as specified by format and provider.
Using Basic ToString() Method
The parameterless ToString() method converts the Int16 value to its default string representation −
using System;
public class Demo {
public static void Main() {
short val1 = 10;
short val2 = -25;
short val3 = 32767;
Console.WriteLine("Value1 = " + val1.ToString());
Console.WriteLine("Value2 = " + val2.ToString());
Console.WriteLine("Value3 = " + val3.ToString());
}
}
The output of the above code is −
Value1 = 10 Value2 = -25 Value3 = 32767
Using ToString() with Format Specifiers
You can use various format specifiers to control how the number appears in the string output −
using System;
public class Demo {
public static void Main() {
short val = 1250;
Console.WriteLine("Default: " + val.ToString());
Console.WriteLine("Currency: " + val.ToString("C"));
Console.WriteLine("Decimal: " + val.ToString("D"));
Console.WriteLine("Exponential: " + val.ToString("E"));
Console.WriteLine("Fixed-point: " + val.ToString("F2"));
Console.WriteLine("Hexadecimal: " + val.ToString("X"));
Console.WriteLine("Custom: " + val.ToString("000000.00"));
}
}
The output of the above code is −
Default: 1250 Currency: $1,250.00 Decimal: 1250 Exponential: 1.250000E+003 Fixed-point: 1250.00 Hexadecimal: 4E2 Custom: 001250.00
Common Format Specifiers
| Format Specifier | Description | Example Output |
|---|---|---|
| C | Currency format | $1,234.00 |
| D | Decimal format | 1234 |
| F | Fixed-point format | 1234.00 |
| X | Hexadecimal format | 4D2 |
| N | Number format with commas | 1,234.00 |
Using ToString() with Custom Format Patterns
Custom format patterns allow precise control over the output format −
using System;
public class Demo {
public static void Main() {
short positive = 42;
short negative = -42;
short zero = 0;
string customFormat = "000;(000);Zero";
Console.WriteLine("Positive: " + positive.ToString(customFormat));
Console.WriteLine("Negative: " + negative.ToString(customFormat));
Console.WriteLine("Zero: " + zero.ToString(customFormat));
Console.WriteLine("Padded: " + positive.ToString("0000"));
Console.WriteLine("With prefix: " + positive.ToString("'NUM:'000"));
}
}
The output of the above code is −
Positive: 042 Negative: (042) Zero: Zero Padded: 0042 With prefix: NUM:042
Conclusion
The Int16.ToString() method provides flexible string conversion capabilities for 16-bit signed integers. Using format specifiers and custom patterns, you can control the exact appearance of numeric values in string format, making it essential for data presentation and formatting requirements.
