JAVA基础学习笔记 day004_作业01(数据类型计算训练)

import java.util.Scanner;

public class WorkDemo01 {

	/*
	 * 需求:
	 * 计算职工实际到手工资
	 * 1.交社保11%
	 * 2.补贴餐补8%,住房补9%
	 */
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
	
		double money = input.nextFloat();
		String result = String.format("%.2f",money);
		System.out.println("工资:" + result + "元");
		
		double a = money * 0.11;
		String a1 = String.format("%.2f",a);
		System.out.println("缴纳社保:" + a1 + "元");
		
		double b = money * 0.08;
		String b1 = String.format("%.2f",b);
		System.out.println("获得餐补:" + b1 + "元");
		
		double c = money * 0.09;
		String c1 = String.format("%.2f",c);
		System.out.println("获得住房补贴:" + c1 + "元");
		
		double sum = money - a + b + c;
		String result1 = String.format("%.2f",sum);
		System.out.println("实际到手工资:" + result1 + "元");
		
	}
}

总结:1.有且只有byte,short,char这三种类型的数据参与运算的时候,先将他们 统一自动类型转换成 int,再参与计算

        2.所以算出的结果通过 String result = String.format("%.2f",num)将结果锁定小数点后2位

猜你喜欢

转载自blog.csdn.net/qq_42801561/article/details/87975361