1

I have this number: double number = 131151201344081895336534324866D;

Console.WriteLine(number) writes 1,31151201344082E+29, and Console.WriteLine(number.ToString("#")) only writes 131151201344082000000000000000.

But I want this number to appear on the console window as: 131151201344081895336534324866.

Is there a way I can print out all the digits?

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
gerebaki
  • 11
  • 1
  • 6
    A `double` has only about 15 significant digits, so it cannot represent the value you gave exactly. `decimal` will give you about 28 significant digits which is better, but still not sufficient four your big number. If your numbers are always integer, you may use [BigInteger](https://learn.microsoft.com/en-us/dotnet/api/system.numerics.biginteger?view=netframework-4.8) – Klaus Gütter Sep 07 '19 at 15:36
  • [Related question](https://stackoverflow.com/questions/15520441/parsing-big-double-value-c-sharp) (well, the answers are relevant). – ProgrammingLlama Sep 07 '19 at 15:39
  • 2
    If you weren't limited by significant digits as indicated above, what would you print for `double d = 1.0/3.0`? It's a good idea to familiarize yourself with the limitations of floating-point representations in computer science (in general, not just for this question). https://floating-point-gui.de/ – Eric J. Sep 07 '19 at 15:44
  • 1
    use `DoubleConverter.ToExactString(number)` with: https://jonskeet.uk/csharp/DoubleConverter.cs – Luuk Sep 07 '19 at 15:46
  • The closest double to the original input is 131151201344081890417389338624. If you really want to see the original string, save the string, not just its double approximation. – Patricia Shanahan Sep 07 '19 at 16:47

1 Answers1

0

import java.math.BigDecimal;

class BigIntegerFact{

public static void main(String args[]) { BigInteger number=new BigDecimal(args[0]); System.out.println(number); }

}

AB7098
  • 36
  • 2