0

i have a variable such this

double a=123456789012345678901234567890.1234567890123456;

i want to convert a(above variable) to string value in

console.writeline();

when i write

a.ToString(); 

i see the result as

1.23456789012346E+29

but only i want to have a varable such below.

string s="123456789012345678901234567890.1234567890123456";

how can i make s from a?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user2352554
  • 521
  • 4
  • 17
  • 2
    use a big decimal class? https://www.nuget.org/packages/BigDecimal/ – Mitch Wheat Jan 11 '19 at 12:24
  • Maybe using a [custom/built-in format](https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?view=netframework-4.7.2#System_Double_ToString_System_String_) will help? – Uwe Keim Jan 11 '19 at 12:24
  • duplicate of ? https://stackoverflow.com/questions/4523741/arbitrary-precision-decimals-in-c-sharp/4524254#4524254 – Mitch Wheat Jan 11 '19 at 12:25
  • 1
    `double` has 64-bit length only; that's why *precision loss* is inevitable. – Dmitry Bychenko Jan 11 '19 at 12:31
  • refer this format and simply apply https://learn.microsoft.com/en-us/dotnet/api/system.double.tostring?redirectedfrom=MSDN&view=netframework-4.7.2#System_Double_ToString_System_String_ – Hitesh Anshani Jan 11 '19 at 13:50

1 Answers1

1

You can't, since double doesn't have such a high precision you can save the entire number into the double. (To see what part is actually saved, try Console.WriteLine(a.ToString("n9"));)

You have to use another data type that does support such high precision numbers, for example BigDecimal.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325