7

Possible Duplicates:
Big integers in C#
C# unlimited significant decimal digits (arbitrary precision) without java

I read the question at Arbitrary precision decimals in C#? but I don't have the J# library. I need a library for arbitrary precision decimals with C#.

Community
  • 1
  • 1
Victor
  • 743
  • 1
  • 5
  • 16
  • 2
    Since neither of the proposed duplicates actually answers the question, I'm voting to reopen. The other answers are just to use `decimal`, `BigInteger`, or the J# libraries. – Gabe Dec 24 '10 at 06:28
  • 1
    The J# runtime library IS a library for arbitrary precision decimals in any .NET language, C# included. – Ben Voigt Dec 26 '10 at 05:44

3 Answers3

14

You could implement your own based on .NET 4.0's BigInteger class. I did this for fun, it does multiplication only:

public struct BigDecimal {
    public BigInteger Integer { get; set; }
    public BigInteger Scale { get; set; }

    public BigDecimal(BigInteger integer, BigInteger scale) : this() {
        Integer = integer;
        Scale = scale;
        while (Scale > 0 && Integer % 10 == 0) {
            Integer /= 10;
            Scale -= 1;
        }
    }

    public static implicit operator BigDecimal(decimal a) {
        BigInteger integer = (BigInteger)a;
        BigInteger scale = 0;
        decimal scaleFactor = 1m;
        while ((decimal)integer != a * scaleFactor) {
            scale += 1;
            scaleFactor *= 10;
            integer = (BigInteger)(a * scaleFactor);
        }
        return new BigDecimal(integer, scale);
    }

    public static BigDecimal operator *(BigDecimal a, BigDecimal b) {
        return new BigDecimal(a.Integer * b.Integer, a.Scale + b.Scale);
    }

    public override string ToString() {
        string s = Integer.ToString();
        if (Scale != 0) {
            if (Scale > Int32.MaxValue) return "[Undisplayable]";
            int decimalPos = s.Length - (int)Scale;
            s = s.Insert(decimalPos, decimalPos == 0 ? "0." : ".");
        }
        return s;
    }
}

...

decimal d1 = 254727458263237.1356246819m;
decimal d2 = 991658834219519273.110324m;
// MessageBox.Show((d1 * d2).ToString()); // OverflowException
BigDecimal bd1 = d1;
BigDecimal bd2 = d2;
MessageBox.Show((bd1 * bd2).ToString()); // 252602734305022989458258125319270.5452949161059356
J.D.
  • 2,114
  • 17
  • 13
  • I suggest using `s.Insert(decimalPos, ".")` instead of calling substring twice and concatenating. – Ben Voigt Dec 24 '10 at 04:46
  • 1
    Also, using `BitInteger` for scale seems overkill. You really think there's a need to support numbers outside the range 10**(-2**63) to bigint * 10**(2**63 - 1) ??? – Ben Voigt Dec 24 '10 at 04:49
  • This is actually pretty good, but `BigDecimal` should probably be a `struct` and, as Ben suggested, the scale should probably be an `int`. – Gabe Dec 24 '10 at 06:32
  • I really liked that idea and completed the draft to support all basic operators, see my answer here: http://stackoverflow.com/a/13813535/804614 – Gigo Dec 11 '12 at 04:23
  • `BigDecimal * BigDecimal` variable not giving correct result. how can we achieve this? Example - `BigDecimal bd1 = new BigDecimal(1234567890123,1234567890123); BigDecimal bd2 = new BigDecimal(1234567890123, 1234567890123); Console.WriteLine("\n" + (bd1 * bd2).ToString()); ` – Oxygen Jul 24 '22 at 04:59
3

Big Decimal:

Big Int (If you like J.D.'s solution, or want to come up with a rational number/fraction type class. That, and I somehow missed that you were looking for decimals, not ints):

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
3

There's always the GNU MP wrapper for .NET as long as you don't mind using the GMP.

BurnsBA
  • 4,347
  • 27
  • 39
Gabe
  • 84,912
  • 12
  • 139
  • 238