Question:
There is a huge number in the form of a text string, 24 decimal places long (for example, 345678923456789876543234
). This number must be divided by another, rather small number (for example, 97
).
How to perform division if even long
and ulong
are not available due to the length of the number?
Answer:
Use the BigInteger
structure:
Console.WriteLine(BigInteger.Parse("345678923456789876543234") / 97);
// выдаёт 3563700241822576046837
Don't forget to include the System.Numerics assembly.
If you need both the quotient and the remainder, then do this:
BigInteger dividend = BigInteger.Parse("345678923456789876543234");
BigInteger quotient = dividend / 97;
BigInteger remainder = dividend % 97;
(or useBigInteger.DivRem
if you like). The remainder can be cast to int
, since it fits there:
int remainder = (int)(dividend % 97);