如何在Java中获得除法结果的完整分数? [英] How to get the full fraction value of a division result in java?

查看:554
本文介绍了如何在Java中获得除法结果的完整分数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java新手,正在使用DrJava IDE进行测试.我有以下分区49700/40000,它显示1.0而不是1.2425.

I am new to Java and I am using DrJava IDE for my testing. I have the following division 49700/40000 and it displays 1.0 instead of 1.2425.

 double t = 49700/40000;
 System.out.println(t);

这是我做错了吗?

推荐答案

尝试一下,这个代替:

double t = 49700/40000.0;
System.out.println(t);

如果两个操作数都是整数,则结果将是一个整数,该整数将被截断,然后将其强制转换为双精度型.相反,如果其中一个操作数是双精度数,则结果将是双精度数.

If both operands are integers the result will be an integer which will be truncated, and then it will be cast in to a double. If, instead, one of the operands is a double, the result will be a double.

这篇关于如何在Java中获得除法结果的完整分数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆