how divided integer is converted to floating point number with decimal

56 views Asked by At

if 123/33 prints out 3 and 3 is an integer if we cast it to float ( (float)123/33 )how do we get the decimal places from the integer 3. does 3 contains floating points internally what ?

 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)
    }
2

There are 2 answers

0
shmosel On BEST ANSWER

The cast does not apply to the entire expression 123/33. You're casting the value 123 to a float, which causes any further operations to use floating-point math.

0
Naman Gala On

123 will be casted(converted) to float and then the division happens, so the result will be float value.

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