函数
/**
计算本息和
@param principal 本金
@param time 时间(天)
@param sevenyearInterestRate 七日年利率
*/
static void CalculateRevenue(double principal,int time,double sevenyearInterestRate){
double principalAndInterest = principal;
double interest = sevenyearInterestRate/365;// 日利率
for (int i = 0; i<time; i++) {
principalAndInterest += principalAndInterest*interest;
}
NSLog(@"本金%f \n 经过%d天\n 本息和%f\n 盈利%f",principal,time,principalAndInterest,(principalAndInterest-principal));
}
使用
// 计算余额宝本息和
double principal = 50000.00f;//本金
int time = 485;//时间
double sevenyearInterestRate = 0.03886f;// 七日年利率
CalculateRevenue(principal,time,sevenyearInterestRate);
log如下
本金50000.000000
经过485天
本息和52649.468744
盈利2649.468744
复杂一点,引入一些定制化的需求(比如每月会取款一定金额)