Float四舍五入保留一位小数点

public class Test {
    public static void main(String[] args){
        double data1 = 3.35;
        float data2 = 3.35f;
        System.out.println(String.format("%.1f", data1));//输出3.4 进行四舍五入小数点
        System.out.println(String.format("%.1f", data2));//输出3.3
        // 强转float为double类型 部分会失真
        System.out.println((double)(data2));//输出3.3499999046325684  所以会导致 第二结果为3.3         System.out.print(strOnePointOfFloat(5.54f)); // output 5.5
        System.out.print(strOnePointOfFloat(5.56f)); // output 5.6
    }     public static String strOnePointOfFloat(float f){
        return String.format("%.1f", Double.valueOf(String.valueOf(f)));
    }
}
05-11 13:23