This question already has answers here:
How to make the division of 2 ints produce a float instead of another int?
                                
                                    (9个答案)
                                
                        
                                6年前关闭。
            
                    
我正在尝试在Java中除以两个数字-更具体地说,我将分数800/9177转换为double或float或w / e。我的问题是,当我尝试获取小数的值时,我得到0。

有人知道该怎么做吗?

我的代码:

double dt = 800/9177;
float ft = 800/9177;
double dw = 800/122;
System.out.println("dt : " + dt + " ft: " + ft + " dw " + dw + " 800/9177 " + (800/9177));


它会打印dt:0 ft:0 dw:6.0 800/9177 0
因此,除dw外,我所有的分数均为0。

最佳答案

最简单的是

double dt = 800.0/9177;


要么

double dt = (double) 800 / 9177;

10-06 10:05