C# Round-trip ("R") Format Specifier

The round-trip ("R") format specifier in C# ensures that a numeric value converted to a string can be parsed back into the same exact numeric value without any precision loss. This format specifier is supported for Single, Double, and BigInteger types.

The "R" specifier is particularly useful when you need to serialize floating-point numbers to strings and then deserialize them back while maintaining perfect accuracy.

Syntax

Following is the syntax for using the round-trip format specifier −

value.ToString("R")
value.ToString("R", CultureInfo.InvariantCulture)

How It Works

The round-trip format specifier automatically determines the minimum number of significant digits needed to ensure that parsing the string representation back to the original numeric type yields the identical value. This prevents precision loss that can occur with other format specifiers.

Round-trip Process Original Value 0.91234582637 String ("R") "0.91234582637" Parsed Value 0.91234582637 ToString("R") Parse() ? Perfect Round-trip

Using Round-trip with Double

Example

using System;
using System.Globalization;

class Demo {
    static void Main() {
        double doubleVal = 0.91234582637;
        string str = doubleVal.ToString("R", CultureInfo.InvariantCulture);
        double resRound = double.Parse(str, CultureInfo.InvariantCulture);
        
        Console.WriteLine("Original: " + doubleVal);
        Console.WriteLine("String: " + str);
        Console.WriteLine("Parsed: " + resRound);
        Console.WriteLine("Round-trip successful: " + doubleVal.Equals(resRound));
    }
}

The output of the above code is −

Original: 0.91234582637
String: 0.91234582637
Parsed: 0.91234582637
Round-trip successful: True

Using Round-trip with Float

Example

using System;
using System.Globalization;

class Demo {
    static void Main() {
        float floatVal = 123.456789f;
        string str = floatVal.ToString("R", CultureInfo.InvariantCulture);
        float resRound = float.Parse(str, CultureInfo.InvariantCulture);
        
        Console.WriteLine("Original float: " + floatVal);
        Console.WriteLine("String representation: " + str);
        Console.WriteLine("Parsed back: " + resRound);
        Console.WriteLine("Values are equal: " + floatVal.Equals(resRound));
    }
}

The output of the above code is −

Original float: 123.4568
String representation: 123.4568
Parsed back: 123.4568
Values are equal: True

Comparison with Other Format Specifiers

Format Specifier Purpose Round-trip Safe
"R" Round-trip formatting Yes
"G" General formatting No (may lose precision)
"F" Fixed-point notation No (limited decimal places)
"E" Scientific notation No (may lose precision)

Conclusion

The round-trip ("R") format specifier guarantees that floating-point values converted to strings can be parsed back to their original values without precision loss. This makes it essential for data serialization scenarios where numeric accuracy must be preserved.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements