DecimalFormat数字格式化

业务需求:
已知一个员工的合同开始时间和结束时间(date类型),要计算出合同的长度,以年为单位,并且保留两位小数 --出自微人事
主要难点在于类型转换和格式化,其中格式化又包括日期格式化和数字格式化

DecimalFormat decimalFormat = new DecimalFormat("##.00");```
public int updateEmp(Employee employee) {
        Date beginContract = employee.getBeginContract();
        Date endContract = employee.getEndContract();
        Double contractTerm = 
                (Double.parseDouble(yearFormat.format(endContract)) - Double.parseDouble(yearFormat.format(beginContract))) * 12 + 
                Double.parseDouble(monthFormat.format(endContract)) - Double.parseDouble(monthFormat.format(beginContract));
        employee.setContractTerm(Double.parseDouble(decimalFormat.format(contractTerm / 12)));
        return empMapper.updateEmp(employee);
    }

参考:
https://www.cnblogs.com/Small-sunshine/p/11648652.html

posted @ 2020-11-17 10:46  yx袁祥  阅读(347)  评论(0)    收藏  举报