Math.ceil和Math.floor返回相同的值 [英] Math.ceil and Math.floor returning same value

查看:124
本文介绍了Math.ceil和Math.floor返回相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个双(23.46)

I have a double (23.46)

使用Math.ceil和Math.floor方法并将我的double解析为这些方法,我得到相同的值返回对我来说,这是23 ...

And using the methods Math.ceil and Math.floor and parsing my double to these methods, I get the same value returned to me, which is 23...

我希望它被四舍五入到24 ..换句话说,如果我有一个15.01的双倍,它应该仍然是舍入到16 ...我该怎么做?

I want it to be rounded off to 24.. In otherwords, if I have a double that's 15.01, it should still be rounded off to 16... How do I do this?

推荐答案

无法重现:

public class Test
{
    public static void main(String[] args)
    {
        System.out.println(Math.ceil(23.46)); // Prints 24
        System.out.println(Math.floor(23.46)); // Prints 23
    }
}

我怀疑要么你没有得到你认为有的输入数据,你没有写出你认为的输出数据。 Math.floor / ceil 本身工作正常。它们返回相同值的唯一时间是输入已经是整数。你谈论解析你的双重...我猜是错误就在那里。请向我们展示一个简短但完整的程序来说明问题。

I suspect that either you haven't got the input data you think you have or you're not writing out the output data you think you are. Math.floor/ceil themselves work fine. The only time they will return the same value is when the input is already an integer. You talk about parsing your double... my guess is that the error lies there. Please show us a short but complete program which demonstrates the problem.

(可能存在非常大的值的其他情况,其中确切的目标整数不能完全表示为一个 double - 我没有检查过 - 但这肯定不是这样的。)

(There may be other scenarios around very large values where the exact target integer can't be represented exactly as a double - I haven't checked - but that's certainly not the case here.)

这篇关于Math.ceil和Math.floor返回相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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