different values for float and double

前端 未结 5 513
独厮守ぢ
独厮守ぢ 2021-01-08 01:16

I don\'t understand why are float values different from double values. From the example bellow it appears that float provides different result than double for the same opera

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-08 01:45

    public class Main {
    
        public static void main(String[] args) {
            float  a=12.6664287277627762f;
            double b=12.6664287277627762;
    
            System.out.println(a);
            System.out.println(b);
        }
    }
    

    Output:

    12.666429
    12.666428727762776
    

    float can handle about 7 decimal places. A double can handle about 16 decimal places.

提交回复
热议问题