p2p一般按年化率来计算投资收益,下面是一个简单的算法计算天标、月标、年标的收益。
public class Arith{
public double computeIncomeAmount(){
double incomeAmount = 0.00;// 标的收益
double apr=15 //年化率
int periodUnit =0 //-1:天标 0:月标 1:年标
int period=1 //投资期限
switch (periodUnit) {
case -1:
incomeAmount = Arith.round(apr / 100 * period * investAmount, 2);
break;
case 0:
incomeAmount = Arith.round(apr / 12 / 100 * period * investAmount,
2);
break;
case 1:
incomeAmount = Arith.round(apr / 360 / 100 * period * investAmount,
2);
break;
}
return incomeAmount;
}
/**
* 功能:提供精确的小数位四舍五入处理。
* @param v 需要四舍五入的数字
* @param scale 小数点后保留几位
* @return 四舍五入后的结果
*/
public static double round(double v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException("精确度不能小于0!");
}
BigDecimal b = new BigDecimal(Double.toString(v));
return b.setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
}