There is a huge number in the form of a text string, 24 decimal places in length (for example, 345678923456789876543234 ). This number should be divided into another, rather small number (for example, 97 ).

How to perform a division, if due to the length of the number even long and ulong not available?

  • But after all, the result of division, for example, by 97 will be an order of magnitude less. Are you worried that the 23-digit result will not fit in, but you are not at all worried about the 24 original digits? - Sergey

3 answers 3

Use the BigInteger structure:

 Console.WriteLine(BigInteger.Parse("345678923456789876543234") / 97); // выдаёт 3563700241822576046837 

Do not forget to connect the assembly System.Numerics.


If you need both private and residual, do this:

 BigInteger dividend = BigInteger.Parse("345678923456789876543234"); BigInteger quotient = dividend / 97; BigInteger remainder = dividend % 97; 

(well, or use BigInteger.DivRem , if you wish). The remainder can be casted into int , since it fits there:

 int remainder = (int)(dividend % 97); 
  • Thank you very much, but I need a form (Windows Forms) and I tried not to bind to a variable ... Tupovot-s ... - Waldemar Karsten
  • @WaldemarKarsten bring the result to a string and then attach where you need it on the form. - Alexey
  • I understand that I express myself somehow messy ... The first time I ask a question and therefore I hope for leniency ... there is a variable. It is necessary to write down a huge number in it and then work with it ... The number is obtained by manipulation ... the texts between themselves are added. 24 numeric characters are obtained. this variable must then be converted to a number. Then it is necessary to obtain module 97 (the remainder of division by 97) from this number and write it into a variable. Here is a problem. If you can help. The numbers will of course be different then ... Therefore, everything is done through variables. Add up the texts and so on - Waldemar Karsten
  • Well, the task of converting text to number and performing an operation is solved by the given code. The variable in which you store the module should be of some strictly defined type? Why don't you make it a BigInteger type? Well, or is string'om at worst and every time it becomes necessary to perform an operation on a number, bring it into BigInteger? If you need working code to solve your problem, alas, it most likely will not be here. - Alexey
  • @WaldemarKarsten: Well, do not output to the console, but write to a variable of type BigInteger , there should be no problems. - VladD

If BigInteger is unavailable

Split the number into parts, for example, divide separately " from left to right ", adding the remainder of dividing the left part to the right, until you reach the most "right" that you divide in the usual way, and combine the result into one number (or a string if it doesn't fit )

  • Yes, exactly as the school was taught to divide the column, and divide and will not need to worry about overflow. - Isaev

Offhand:

 using System.Numerics; ... //Ваша строка, полученная путем манипуляций. string yourStringNumber = "345678923456789876543234"; //Преобразуем строку в число. BigInteger big = BigInteger.Parse(yourStringNumber); //это ваш делитель, равный сейчас 97. int divider = 97; //Получаем остаток от деления на 97.... Ну или что у вас там будет в переменную divider записано. BigInteger result = big % divider; //Вывод результата строкой на форму, в контрол label1 или... label1.Text = result.ToString(); //...Показываем число строкой в отдельном модальном окне. И так далее.... MessageBox.Show(result.ToString()); ...