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
"." custom specifier in C#
The "." custom format specifier in C# adds a localized decimal separator into the output string. It determines the exact position where the decimal point will appear in the formatted numeric value.
The first period in the format string determines the location of the decimal separator in the formatted value. Any additional periods are ignored and treated as literal characters.
Syntax
Following is the syntax for using the "." custom format specifier −
number.ToString("format_with_decimal_point")
String.Format("format_with_decimal_point", number)
Where the format string contains a period (.) to specify decimal separator placement.
How It Works
The "." specifier uses the decimal separator character defined by the current culture. In invariant culture, it uses a period (.), while other cultures may use different characters like a comma (,).
Using "." with Different Decimal Places
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
double d;
d = 3.7;
Console.WriteLine(d.ToString("0.00", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:0.00}", d));
d = 5.89;
Console.WriteLine(d.ToString("00.00", CultureInfo.InvariantCulture));
Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:00.00}", d));
}
}
The output of the above code is −
3.70 3.70 05.89 05.89
Using "." with Culture-Specific Formatting
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
double value = 1234.56;
// US culture uses period as decimal separator
CultureInfo usCulture = new CultureInfo("en-US");
Console.WriteLine(value.ToString("0.00", usCulture));
// German culture uses comma as decimal separator
CultureInfo germanCulture = new CultureInfo("de-DE");
Console.WriteLine(value.ToString("0.00", germanCulture));
// French culture uses comma as decimal separator
CultureInfo frenchCulture = new CultureInfo("fr-FR");
Console.WriteLine(value.ToString("0.00", frenchCulture));
}
}
The output of the above code is −
1234.56 1234,56 1234,56
Multiple Decimal Points
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
double value = 42.75;
// Only first period acts as decimal separator
Console.WriteLine(value.ToString("00.00.extra", CultureInfo.InvariantCulture));
// Additional periods are treated as literal characters
Console.WriteLine(value.ToString("0.0..0", CultureInfo.InvariantCulture));
}
}
The output of the above code is −
42.75.extra 42.7..5
Conclusion
The "." custom format specifier in C# controls the placement of the decimal separator in formatted numeric output. It adapts to the culture-specific decimal separator character, making it essential for internationalized applications that need proper numeric formatting across different locales.
