C# Round-trip ("R") Format Specifier
Description
The round-trip ("R") format specifier ensures the formatted string can be converted back.
Item | Value |
---|---|
Name | Round-trip |
Format specifier | "R" or "r" |
Supported by | Single, Double, and BigInteger. |
Result | A string that can round-trip to an identical number. |
Precision specifier | Ignored. |
Example,
Value | Format | Formatted |
---|---|---|
123456789.12345678 | ("R") | 123456789.12345678 |
-1234567890.12345678 | ("R") | -1234567890.1234567 |
The result string is affected by the following NumberFormatInfo properties.
- NegativeSign
- NumberDecimalSeparator
- PositiveSign
Example
using System;//w w w . ja v a 2 s . c o m
using System.Globalization;
class MainClass
{
static void Main()
{
double value;
value = Math.PI;
Console.WriteLine(value.ToString("r"));
// Displays 3.1415926535897931
Console.WriteLine(value.ToString("r",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays 3,1415926535897931
value = 1.623e-21;
Console.WriteLine(value.ToString("r"));
// Displays 1.623E-21
}
}
The code above generates the following result.