-1

I have the following code to convert a very long numeric string.

using System;

class MainClass {
  public static void Main (string[] args) {
    string longString = "1000000000000000000000000000001";
    double convertedString = Double.Parse(test);
    Console.WriteLine(test2);
  }
}

However, convertedString is outputted in scientific notation:

1E+30

Is there a way to retain the exact value of the double when I convert it from a string?

ddastrodd
  • 710
  • 1
  • 10
  • 22

1 Answers1

1

The format of the output and the precision of the variable itself have nothing to do with each other. You can get the format you want by changing the format string, e.g.

Console.WriteLine("{0:N}", test1);

As for the precision of the variable, you should be aware that floating point numbers are not precise. And your number is about ten digits too long to fit into a long.

You will either need to store that number as a custom data type or, if it is just an identifier and not actually a number on which you need to perform math, simply store it as a string.

John Wu
  • 50,556
  • 8
  • 44
  • 80