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
Double.ToString Method in C#
The Double.ToString() method in C# is used to convert the numeric value of this instance to its equivalent string representation. This method provides several overloads to format the double value in different ways, including culture-specific formatting and custom format strings.
Syntax
Following is the syntax for the basic ToString() method −
public override string ToString();
Following is the syntax for ToString() with format specifier −
public string ToString(string format);
Following is the syntax for ToString() with culture-specific formatting −
public string ToString(IFormatProvider provider);
Return Value
The method returns a string that represents the value of the current Double instance.
Using Basic ToString() Method
Example
using System;
public class Demo {
public static void Main() {
double d1 = 45.7878d;
double d2 = -68.89d;
string str1 = d1.ToString();
string str2 = d2.ToString();
Console.WriteLine("Positive number: " + str1);
Console.WriteLine("Negative number: " + str2);
}
}
The output of the above code is −
Positive number: 45.7878 Negative number: -68.89
Using ToString() with Format Specifiers
Example
using System;
public class Demo {
public static void Main() {
double d = 1234.5678d;
Console.WriteLine("Default: " + d.ToString());
Console.WriteLine("Fixed-point (F2): " + d.ToString("F2"));
Console.WriteLine("Currency (C): " + d.ToString("C"));
Console.WriteLine("Scientific (E): " + d.ToString("E"));
Console.WriteLine("Percentage (P): " + (0.85).ToString("P"));
}
}
The output of the above code is −
Default: 1234.5678 Fixed-point (F2): 1234.57 Currency (C): $1,234.57 Scientific (E): 1.234568E+003 Percentage (P): 85.00%
Using ToString() with Culture-Specific Formatting
Example
using System;
using System.Globalization;
public class Demo {
public static void Main() {
double price = 1299.99d;
CultureInfo usCulture = new CultureInfo("en-US");
CultureInfo germanCulture = new CultureInfo("de-DE");
Console.WriteLine("US format: " + price.ToString("C", usCulture));
Console.WriteLine("German format: " + price.ToString("C", germanCulture));
Console.WriteLine("Invariant culture: " + price.ToString(CultureInfo.InvariantCulture));
}
}
The output of the above code is −
US format: $1,299.99 German format: 1.299,99 ? Invariant culture: 1299.99
Common Format Specifiers
| Format Specifier | Description | Example Output |
|---|---|---|
| F or f | Fixed-point notation | 123.46 |
| E or e | Scientific notation | 1.23E+002 |
| C or c | Currency format | $123.46 |
| P or p | Percentage format | 12.35% |
| N or n | Number format with commas | 1,234.56 |
Conclusion
The Double.ToString() method provides flexible string conversion with support for various format specifiers and culture-specific formatting. Use format specifiers like "F2" for fixed decimal places, "C" for currency, or "E" for scientific notation to control the output format according to your application's needs.
