Convert double to string keeping the same format

I need to convert a value of type double to string in C#, without that value being changed to scientific notation.

My code is:

double valor1 = 0.000024746578864525161;

string resultado = Convert.toString(valor1);

My output is: 2,8192381923 and-15

I wanted the output to be exactly the same Plus in string

Output : "0.000024746578864525161";

The reasons why I need the value not to be expressed in scientific notation are:

1 - I'm reading an XLSX.

2-I am doing validation of the values entered by the user. And in this validation, I cannot allow invalid characters.

3-My string is subjected to a Regex.

Regex(@"[;!*#&@?()'$~^<>ºª%\{}A-Za-z]");

4-the fact that my Regex does not accept characters causes the number expressed in notation 2,8192381923 E-15 to become invalid.

Is there a way to do the conversion of:

 double varlor = 0.000024746578864525161;

For

string resultado = "0.000024746578864525161"

And not for scientific notation:

string resultado = "2,8192381923E-15"
Author: Maniero, 2018-06-15

1 answers

It is not possible to do exactly like this, but something close, can form like this:

valor1.ToString("#########0.000000000000000000000000")

Will notice that it does not take all possible digits, no matter how many houses it puts. The type double has limited accuracy. If you want more accuracy you should use a decimal. It can for 339 houses, I don't know why to do this, and it will not have more accurate result. Take the test and you will see that it goes to 0.0000247465788645252.

If you want greater accuracy then you must give up accuracy and scientific notation is appropriate.

O Excel works well with scientific notation. If that's not what you want, the problem seems to be another.

Do not confuse the number with numeric representation. When you convert to string you're dropping the number and taking its representation, which are different things.

Of course, you can do a manual conversion, but it takes a lot of work and it's not easy to do right.

Maybe I wanted to do something else, but we have no way of knowing by the question. If the concept is wrong any technical response will be bad.

 3
Author: Maniero, 2020-07-22 17:28:14