分整数如何转换为带小数点的浮点数

how divided integer is converted to floating point number with decimal

如果 123/33 打印出 3 并且 3 是一个整数,如果我们将其转换为浮点数 ((float)123/33 )我们如何从整数 3 中获取小数位。3 内部是否包含浮点数? ?

 public static void test() {
        System.out.println("==========");
        System.out.println(123/33); // prints 3 
        System.out.println((float)123/33); // prints 3.7272727 
        //so if we cast it to float we get the decimal points also (.7272727)
    }

123casted(转换)为 float 然后进行除法,因此结果将是浮点值。

System.out.println("==========");
System.out.println(123/33); // prints 3 
System.out.println((float)123); // prints 123.0 
System.out.println((float)123/33); // prints 3.7272727 

转换不适用于整个表达式 123/33。您将值 123 转换为浮点数,这会导致任何进一步的操作都使用浮点数学。