Search code examples
c#mathbigintegercalculationint128

C# BigInteger with Decimal outcome


I'm trying to do calculations with huge numbers. for example:

22352794900029489130063309832192 / 1000000000000000000000000000000

I want the outcome to be 22.35279490 (preferred decimal), but if I use BigInteger the result gets rounded to 22.

Does .NET have something similar to an uint128?

What´s the best practise to do so?


Solution

  • To preserve precision, consider converting the BigInteger values to doubles first, then dividing after.

    BigInteger a = BigInteger.Parse("22352794900029489130063309832192");
    BigInteger b = BigInteger.Parse("1000000000000000000000000000000");
    double da = (double)a;
    double db = (double)b;
    double result = da / db;