Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string from large double value(C#)

Can't find simple way to convert double to string. I need to convert large numbers without distortion. Such as:

double d = 11111111111111111111;
string s = d.ToString();
Console.WriteLine(s);
//1.11111111111111E+19

How to get string value from double value exactly the same as user enter.

11111111111111111111111 => "11111111111111111111111"

1.111111111111111111111 => "1.111111111111111111111"

Any ideas how it can be done?

like image 834
Artem Kyba Avatar asked Nov 02 '16 09:11

Artem Kyba


People also ask

How to convert a string to double in C++?

Here is an example to convert a string to double. In the above program, a char type array s [20] is declared which is initialized with a alphanumeric characters. The function strtod () is used to convert that string into a double number.

How to convert double to string or character array with custom precision?

We can convert double to string or a character array with custom precision by specifying the precision in sprintf as below. Using sprintf we can add extra text (as per required) to the string at the same time.

What is a double variable in C?

In C double is a keyword for the double data type. It represents floating point numbers with better precision. Usually we associate the name with the idea that it has double the precision of the float type. Declaring a variable is just like with any other type: However, in C by default all floating point values are interpreted as double.

How do you represent a double in C?

Representation of double in C. In C double’s exact precision depends on the implementation. Most compilers today use the IEEE-754 standard. To represent the numbers they use 64 bits as follows: The first bit stands for the sign. 1 means negative, 0 means positive. 52 bits of significand (mantissa)


3 Answers

double is a floating point type. So it has a limited accuracy. In your example, you could do something like this:

double d = 11111111111111111111;
string s = d.ToString("F0");
Console.WriteLine(s);

But as you'll see,this would output 11111111111111100000 instead of 11111111111111111111,so it has lost accuracy in the process. So the answer here is use the right type for the work. If you need a string, use a string variable to store the value.

Edit

This was the question i was trying to find that explains the problem with floating point math., thanks to @GSerg

like image 139
5 revs, 4 users 77%SuperG Avatar answered Oct 23 '22 04:10

5 revs, 4 users 77%SuperG


First of all: 11111111111111111111111 is to large for a double value and also this value: 1.111111111111111111111 since the double max decimal length is 17.

By default, a Double value contains 15 decimal digits of precision, although a maximum of 17 digits is maintained internally.

For this reason you should use BigInteger and then ToString for formatting the output.
There is also a library in the nuget Directory called BigRational, never used and seems in Beta stage but probably will help in solving this problem.

like image 33
Tinwor Avatar answered Oct 23 '22 02:10

Tinwor


In general case, you can't do this: user can well input, say 123, in many a way:

  • 123
  • 123.00
  • 1.23e2
  • 12.3E1
  • 123.0e+00
  • 1230e-1

etc. When you convert the user input into double you loose the initial format:

string userInput = ...

// double is just 123.0 whatever input has been
double value = double.Parse(userInput);

In case you want to drop exponent if it's possible you can

double value = 11111111111111111111;

string result = value.ToString("#######################");

And, please, notice, that double has 64 bit to store the value, that's why a distortion is inevitable for large numbers:

// possible double, which will be rounded up
double big = 123456789123456789123456789.0;

// 1.2345678912345679E+26
Console.WriteLine(big.ToString("R"));
// 123456789123457000000000000
Console.WriteLine(big.ToString("###########################")); 

May be you want BigInteger instead of double:

using System.Numerics;

...

BigInteger value = BigInteger.Parse("111111111111111111111111111111111"); 

// 111111111111111111111111111111111
Console.WriteLine(value.ToString());
like image 28
Dmitry Bychenko Avatar answered Oct 23 '22 04:10

Dmitry Bychenko