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);
    }
    如有不当,欢迎指正
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号