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
Int64.ToString() Method in C#
The Int64.ToString() method in C# is used to convert a 64-bit integer (long) value to its equivalent string representation. This method provides multiple overloads for different formatting options, allowing you to control how the numeric value appears as text.
Syntax
Following are the primary syntax overloads for the Int64.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 how the value should be formatted (optional).
provider − An object that supplies culture-specific formatting information (optional).
Return Value
Returns a string representation of the Int64 value based on the specified format and culture settings.
Using Basic ToString() Method
The parameterless ToString() method converts the integer to its standard string representation −
using System;
public class Demo {
public static void Main(){
long val1 = 0;
long val2 = Int64.MaxValue;
long val3 = -123456789;
Console.WriteLine("Value1 = " + val1.ToString());
Console.WriteLine("Value2 = " + val2.ToString());
Console.WriteLine("Value3 = " + val3.ToString());
}
}
The output of the above code is −
Value1 = 0 Value2 = 9223372036854775807 Value3 = -123456789
Using ToString() with Format Strings
The ToString(string format) overload allows you to specify how the number should be formatted using standard numeric format strings −
using System;
public class Demo {
public static void Main(){
long val1 = 87987687;
long val2 = 35436367;
Console.WriteLine("Decimal with 8 digits: " + val1.ToString("D8"));
Console.WriteLine("Hexadecimal: " + val2.ToString("X"));
Console.WriteLine("Currency format: " + val1.ToString("C"));
Console.WriteLine("Number with commas: " + val2.ToString("N0"));
}
}
The output of the above code is −
Decimal with 8 digits: 87987687 Hexadecimal: 21CB74F Currency format: $87,987,687.00 Number with commas: 35,436,367
Common Format Strings
| Format String | Description | Example Result |
|---|---|---|
| D or d | Decimal integer | 12345 |
| X or x | Hexadecimal | 3039 (uppercase) or 3039 (lowercase) |
| N or n | Number with thousand separators | 1,234,567 |
| C or c | Currency format | $1,234.56 |
Using ToString() with Culture-Specific Formatting
You can also specify culture-specific formatting using IFormatProvider −
using System;
using System.Globalization;
public class Demo {
public static void Main(){
long value = 1234567890;
Console.WriteLine("US format: " + value.ToString("N", CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine("German format: " + value.ToString("N", CultureInfo.CreateSpecificCulture("de-DE")));
Console.WriteLine("French format: " + value.ToString("N", CultureInfo.CreateSpecificCulture("fr-FR")));
}
}
The output of the above code is −
US format: 1,234,567,890.00 German format: 1.234.567.890,00 French format: 1 234 567 890,00
Conclusion
The Int64.ToString() method provides flexible string conversion for 64-bit integers with support for various format strings and culture-specific formatting. Use format strings like "D", "X", "N", and "C" to control the output representation, and specify culture providers for internationalization requirements.
