private int GetCurrentPercentUL() { var maxItem = 100153UL; var currentItem = 100150UL; return (int)(currentItem / (maxItem / 100M)); } private int GetCurrentPercentBI() { var maxItem = new BigInteger(100153); var currentItem = new BigInteger(100150); return (int)(currentItem / (maxItem / 100)); } 

There was a variant with ulong and decimal (100M), it worked quite correctly. Then I had to increase the numerical range to BigInteger, and there was a problem with the calculation of interest. How to correctly and beautifully alter the GetCurrentPercentBI method to work correctly?


Upd:

 private int GetCurrentPercentBI() { var maxItem = new BigInteger(100153); var currentItem = new BigInteger(100150); var percent = (int)(currentItem / (maxItem / new BigInteger(100))); if (percent == 100 && maxItem > currentItem) percent = 99; return percent; } 

There is such a solution, but for me it is ugly. If there are more correct options - write in the answers.

  • You lose type. As an option, bring all the numbers to BigInt (namely, change 100 to BigInteger(100) ), and then cast the result to int. - nick_n_a
  • @nick_n_a, no, it did not help. Please note that the GetCurrentPercentUL method returns 99, and the GetCurrentPercentBI method returns 100. It is also not possible to enable Math because it does not support the BigInteger type. - User2398471
  • currentItem * 100 / maxItem - PetSerAl

0