最近想给爸妈购买养老储蓄型保险,为了对比是否划算,简单的写了一个复利计算算法,留作记录。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "复利计算器"
btnCalculateCompound.setOnClickListener {
if (TextUtils.isEmpty(etCapital.text) || TextUtils.isEmpty(etInterestRate.text) || TextUtils.isEmpty(etYears.text)) {
return@setOnClickListener
}
tvMoney.text = calculateCompoundInterest(etYears.text.toString().toInt()
, etInterestRate.text.toString().toDouble()
, etCapital.text.toString().toDouble()).toString()
}
btnCalculateYearsCompound.setOnClickListener {
if (TextUtils.isEmpty(etCapital.text) || TextUtils.isEmpty(etInterestRate.text) || TextUtils.isEmpty(etYears.text)) {
return@setOnClickListener
}
tvMoney.text = calculateYearsCompoundInterest(etYears.text.toString().toInt()
, etInterestRate.text.toString().toDouble()
, etCapital.text.toString().toDouble(), etContinueYears.text.toString().toInt()
?: 0).toString()
}
}
/**
* 单纯的算复利
*/
fun calculateCompoundInterest(years: Int, interestRate: Double, capital: Double): Double {
var currentYear = 0
var currentMoney = capital
while (currentYear < years) {
currentMoney = calculateInterestRates(interestRate, currentMoney)
currentYear++
}
return currentMoney
}
/**
* 计算一年的利息
*/
fun calculateInterestRates(interestRate: Double, capital: Double): Double {
return capital * (1 + interestRate)
}
/**
* 计算连续多年存同样的钱,一段时间之后的复利计算
*/
fun calculateYearsCompoundInterest(years: Int, interestRate: Double, capital: Double, continueYears: Int): Double {
var currentYear = 0
var currentMoney = capital
if (years <= continueYears) {
while (currentYear < years) {
currentYear++
if (years == 1) {
currentMoney = calculateInterestRates(interestRate, currentMoney)
} else {
currentMoney = calculateInterestRates(interestRate, currentMoney) + calculateInterestRates(interestRate, capital)
}
}
} else {
while (currentYear < continueYears) {
currentYear++
if (years == 1) {
currentMoney = calculateInterestRates(interestRate, currentMoney)
} else {
currentMoney = calculateInterestRates(interestRate, currentMoney) + calculateInterestRates(interestRate, capital)
}
}
var currentYearAftercontinueYears = 0
while (currentYearAftercontinueYears < years - continueYears) {
currentYearAftercontinueYears++
currentMoney = calculateInterestRates(interestRate, currentMoney)
}
}
return currentMoney
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/etCapital"
android:layout_width="match_parent"
android:layout_height="44dp"
android:hint="本金"/>
<EditText
android:id="@+id/etInterestRate"
android:layout_width="match_parent"
android:layout_height="44dp"
android:hint="年利率"/>
<EditText
android:id="@+id/etYears"
android:layout_width="match_parent"
android:layout_height="44dp"
android:hint="年期"/>
<EditText
android:id="@+id/etContinueYears"
android:layout_width="match_parent"
android:layout_height="44dp"
android:hint="连续不间断多少年,每年固定存款"/>
<Button
android:id="@+id/btnCalculateCompound"
android:layout_width="match_parent"
android:layout_height="44dp"
android:text="计算复利"/>
<Button
android:id="@+id/btnCalculateYearsCompound"
android:layout_width="match_parent"
android:layout_height="44dp"
android:text="计算每年固定存固定钱数,多年后所得利息"/>
<TextView
android:id="@+id/tvMoney"
android:layout_width="match_parent"
android:layout_height="44dp"
/>
</LinearLayout>