CKTIS / C#
Double.ToString — formatting a number with an explicit provider
Double.ToString(string, IFormatProvider) converts a double value to text while letting you choose the numeric format and cultural rules separately. If the provider's GetFormat method returns null when asked for NumberFormatInfo, formatting falls back to the current culture.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
public string ToString(string? format, IFormatProvider? provider);Double.ToString(string, IFormatProvider) returns a text representation of a double value. The format parameter selects the notation, while provider supplies cultural rules, usually through CultureInfo or NumberFormatInfo.
ToString() uses "G" and the current culture. ToString(string) changes only the format, while ToString(IFormatProvider) changes only the provider of formatting rules. In the two-argument overload, a null or empty format means "G"; a null provider means the current culture.
"F2" produces two digits after the decimal separator, while "N2" also groups digits. Formatting can round the resulting text but does not change the stored value. In .NET 10, an unsupported numeric format specifier such as "D" causes FormatException for finite double values. For NaN and positive or negative infinity, the method returns the corresponding culture-specific symbol before validating the specifier, so "D" does not throw for those values.
The example uses CultureInfo.InvariantCulture and a clone of its formatting rules with a different decimal separator. This demonstrates the difference between 1234.50 and 1234,50 without depending on the computer's regional settings.
Examples
using System;
using System.Globalization;
double value = 1234.5;
var commaFormat = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
commaFormat.NumberDecimalSeparator = ",";
Console.WriteLine(value.ToString("F2", CultureInfo.InvariantCulture));
Console.WriteLine(value.ToString("F2", commaFormat));Result
1234.50 1234,50
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.