class FIRST
{
public static void main()
{
float f1 = 8/10;
System.out.println(f1);
}
}
output: 0.0 in case of f1 type float
output: 0 in case of f1 type int
but when I am doing this on a calculator , the answer is coming 0.8
I am confused.Is there quotient and remainder calculation different in computer as compare with math.
但是当我使用在线计算器做8/10时,它给出了余数8和商0.online计算器给出了商和余数的正确答案。
您正在执行整数数学运算,因此8/10将为0,因为它是整数并且会删除分数。然后,它将0分配给浮点数。因此,添加.f(用于float)并执行以下操作:
或双
如果要划分两个int变量并分配给浮点类型,则需要将一个强制类型转换为该类型。