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

ProgrammingServer Side ProgrammingCsharp

This round-trip ("R") format specifier is supported for the Single, Double, and BigInteger types.

It ensures that a numeric value converted to a string is parsed back into the same numeric value.

Let us see an example −

Firstly, we have a double variable.

double doubleVal = 0.91234582637;

Now, use the ToString() method: and set the Round-trip format specifier.

doubleVal.ToString("R", CultureInfo.InvariantCulture);

Let us see the complete example −

Example

 Live Demo

using System;
using System.Numerics;
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);
      // round-trip Double with 'R'
      Console.WriteLine(doubleVal.Equals(resRound));
   }
}

Output

True
raja
Published on 11-Sep-2018 08:43:55
Advertisements