This is a fairly complex calculation, which is usually part of the company's intellectual property. Therefore, I doubt someone will send the code. I went along this road, and it requires enormous trials, depending on how far you decide to go with it.
You might want to check out the following document. It has a lot of math on how to get there.
Plotting depreciation
change
Just add a couple of things. When performing calculations in code, it is extremely important to use a data type, such as Decimal, instead of floating point types, such as double. A decimal value has been explicitly created for these types of cash settlements. Floating-point types will cause a lot of rounding errors, which will cause the calculated values ββto be disabled by unacceptable sums.
Further, the mortgage calculators that you find on the Internet are of high quality. When testing your method, it will be useful to find out what online calculators came up with, but in no case do not consider them more accurate than yours. As a rule, they can clearly see if you are in the right chalet, but they can be turned off by 0.1% per year of the loan term.
Final note
Ok, this is the last edit. You might want to purchase a library from a company, such as Math Corp, instead of collapsing your own. I am sure it will be accurate and much cheaper than dev / qa time to get your right.
Notme source share