Published Date : 2021年1月13日22:57

Part 2 - Pythonを使って複利による利息の計算をするアプリを作ろう
Part 2 - Create an app that calculates compound interest using Python


This blog has an English translation


ニコニコ動画にアップした動画のまとめ記事です。

This is a summary blog post about a video I uploaded to NicoNico.

細かい部分は動画を参考にしてください。

Please refer to the video for details.


目次

Table of Contents




① 動画の説明
① Video Description



警告:

このアプリは大雑把に大体の見積りを計算して楽しむ為のツールです。

計算の詳細や、端数の処理等は各金融機関等によって異なります。

このアプリを使って何かしらの損害が発生したとしても、私は一切の責任を負いません。

このアプリを使って何かしらの損害が発生したとしても、私は一切の責任を負いません。

Warning:

This application is used to calculate rough estimates and enjoy them.

The details of calculations and the treatment of fractions differ depending on the financial institution.

​I will not be liable for any damage caused by using this app.

結果を分かりやすく表示させる為、区切り文字を間に挿入します。

To display the results more clearly, a delimiter is inserted between them.

if 複利方式 == '1':
        print('\n' + '-' * 9)
        print('月複利')
        print('-' * 9 + '\n')

最初は月ごとの利息繰り入れ方式の処理を書いていきます。

At first, we will write the monthly interest transfer method.

for 月 in range(1, 積立期間月 + 1):

そしてFor文を使って一回ずつ計算とその結果を表示させるようにします。

Then use the For statement to display the calculation and its results one at a time.

if 利息端数 == '1':
    利息 = 利息 + int(繰り入れ後元金 * 月利)
    税引き後利息 = int(利息 * 税率)

利息の端数処理を行い、実際の計算を始めます。

Rounds the interest and starts the actual calculation.

利息 = Decimal(str(利息)).quantize(
    Decimal('0'), rounding=ROUND_HALF_UP)

利息の繰り入れが期初か期末かどうかを判定して、計算結果を文字列フォーマットを利用して変数に足し合わせていきます。

We determine whether the interest transfer is at the beginning or end of the period and the result is added to the variable using the string format.

if 利息組み込み == '1':
    print('\n< 利息組み込み 期初 >')

桁数によるカンマ区切りと小数点以下の処理についても細かく指定して読みやすくします。

The number of digits used to separate commas and decimals is also specified in detail for better readability.

結果 += f'税引き後元利合計:{税引き後元利合計:,} '

最後に次の月へ元本を反映させるようにします。

Finally, we will reflect the principal to the next month.

元金 = 元金 + 毎月の積立金
繰り入れ後元金 = 元金 + 税引き後利息

次に四半期、半年、年ごとの複利繰り入れの場合の処理を書きます。

Next, We will write a script about the process of quarterly, semi-annual, and yearly compounding.

else:
    print('\n' + '-' * 9)

同じ様に利息の繰り入れを期初と期末に場合において処理を分けてスクリプトを記述します。

In the same way, we write a script for carrying in interest at the beginning of the period and at the end of the period separately.

剰余演算を使用して、前の月と、利息を繰り入れる月の処理を分けていきます。

The remainder operation is used to separate the previous month from the month in which interest is carried.

同じように利息の端数を計算して、前回の説明の通りに課税する場合やそうでない場合とで分けて計算処理を書いていきます。

Similarly, the rounding up or down of interest is calculated and, as explained previously, write the calculation process separately according to the case where the tax is imposed or not.

後は細かい微調整を行っていきます。

After that, we will make fine adjustments and finish the script.

実質利率の計算方法は以下のようになります。

Actual interest rates are calculated as follows.

端数切り捨てはintメソッドを使用し、四捨五入はdecimalモジュールのquantizeメソッド、そして端数切り上げはmathモジュールのceilメソッドを使用します。

Fraction interest is truncated using the int method, rounding off is done using the quantize method of the decimal module, and rounding up is done using the ceil method of the math module.

面倒ではありますが、同じ内容の処理をコピペしていきます。

It is troublesome, but I will copy and paste the same process.

再度細かく調整していきます。

I will adjust it finely again.

最後にユーザーからの入力部分のコメントアウトを解除して、スクリプトの挙動を確かめてみましょう。

Finally, let's uncomment the input from the user and see how the script behaves.

try:
    年利 = float(input('年利を入力(%) ')) / 100
    初期金額 = int(input('初期金額を入力(万円) ')) * 10000
    毎月の積立金 = int(input('毎月の積立金を入力(万円) ')) * 10000
    積立年数 = int(input('積立年数(年) '))
    # 年利 = 3 / 100
    # 初期金額 = 30 * 10000
    # 毎月の積立金 = 30 * 10000
    # 積立年数 = 3
except:
    print('数値を入力してね')
    sys.exit()


以上です。お疲れ様です。

That's all. Thank you for your hard work.

各自で創意工夫してもっと良いアプリに仕上げてください。

Please devise your own ideas and make it a better application.