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.ToString() Method in C#
The Decimal.ToString() method in C# is used to convert the numeric value of a decimal instance to its equivalent string representation. This method provides various overloads to format the decimal value according to specific requirements.
Syntax
Following is the basic syntax for Decimal.ToString() method −
public override string ToString();
The method also has overloaded versions for custom formatting −
public string ToString(string format); public string ToString(IFormatProvider provider); public string ToString(string format, IFormatProvider provider);
Parameters
format − A standard or custom numeric format string (optional).
provider − An object that supplies culture-specific formatting information (optional).
Return Value
The method returns a string representation of the decimal value.
Using Basic ToString() Method
Example
using System;
public class Demo {
public static void Main() {
decimal d1 = 3444.787m;
decimal d2 = 100m;
decimal d3 = 0.123456789m;
string str1 = d1.ToString();
string str2 = d2.ToString();
string str3 = d3.ToString();
Console.WriteLine("String 1 = " + str1);
Console.WriteLine("String 2 = " + str2);
Console.WriteLine("String 3 = " + str3);
}
}
The output of the above code is −
String 1 = 3444.787 String 2 = 100 String 3 = 0.123456789
Using ToString() with Format Specifiers
Example
using System;
public class Demo {
public static void Main() {
decimal value = 1234.5678m;
Console.WriteLine("Default: " + value.ToString());
Console.WriteLine("Currency (C): " + value.ToString("C"));
Console.WriteLine("Fixed-point (F2): " + value.ToString("F2"));
Console.WriteLine("Number (N): " + value.ToString("N"));
Console.WriteLine("Percentage (P): " + value.ToString("P"));
Console.WriteLine("Custom (0000.00): " + value.ToString("0000.00"));
}
}
The output of the above code is −
Default: 1234.5678 Currency (C): $1,234.57 Fixed-point (F2): 1234.57 Number (N): 1,234.57 Percentage (P): 123,456.78 % Custom (0000.00): 1234.57
Common Format Specifiers
| Format Specifier | Description | Example Output |
|---|---|---|
| C or c | Currency format | $1,234.57 |
| F or f | Fixed-point format | 1234.57 |
| N or n | Number format with commas | 1,234.57 |
| P or p | Percentage format | 123,456.78% |
Using ToString() with Culture-Specific Formatting
Example
using System;
using System.Globalization;
public class Demo {
public static void Main() {
decimal value = 1234.56m;
CultureInfo usCulture = new CultureInfo("en-US");
CultureInfo germanCulture = new CultureInfo("de-DE");
Console.WriteLine("US Format: " + value.ToString("C", usCulture));
Console.WriteLine("German Format: " + value.ToString("C", germanCulture));
Console.WriteLine("Invariant Culture: " + value.ToString(CultureInfo.InvariantCulture));
}
}
The output of the above code is −
US Format: $1,234.56 German Format: 1.234,56 ? Invariant Culture: 1234.56
Conclusion
The Decimal.ToString() method is essential for converting decimal values to string representations. It provides flexible formatting options through format specifiers and culture-specific formatting, making it suitable for various display and localization requirements in C# applications.
