C# Round-Trip R Format Specifier



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
Updated on: 2020-06-22T15:39:31+05:30

859 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements