代码:
package com.pan.domain;
import java.text.DecimalFormat;
import java.text.ParseException;
public class AverageCalc {
/**
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException {
//总金额 500
//本金 600.6
//赠送金额 300.5
double a=600.6d;
double b=100.5d;
double sum=a+b;
double temp1=a/sum;
double temp2=b/sum;
System.out.println(temp1);
System.out.println(temp2);
System.out.println("比例:");
int percent1=(int)(temp1*100);
int percent2=(int)(temp2*100);
System.out.println(percent1+"% - "+percent2+"%");
double value1=500*temp1;
double value2=500*temp2;
System.out.println("结果:");
System.out.println(value1+" - "+value2);
DecimalFormat format=new DecimalFormat("#.00");
System.out.println("保留两位小数");
System.out.println(format.format(value1)+" - "+format.format(value2));
value1=Double.parseDouble(format.format(value1));
value2=Double.parseDouble(format.format(value2));
System.out.println("误差:");
System.out.println(500-(value1+value2));
Double ad=0d;
Double bd=0d;
System.out.println(ad.equals(bd));
}
}
输出结果:
0.8566538296961917
0.1433461703038083
比例:
85% - 14%
结果:
428.3269148480959 - 71.67308515190415
保留两位小数
428.33 - 71.67
误差:
0.0
true