为什么我的浮点变量保存的是整数?

发布于 2024-12-05 18:51:45 字数 1693 浏览 1 评论 0原文

我已经浏览这段代码有一段时间了,我似乎无法弄清楚可能简单的错误是什么......简而言之,我在Java中有一个浮点变量,它似乎只存储整数内容(整数)实际应有的值。之前,当我将所有内容都塞进一个函数中时,这段代码可以正常工作,但是在我重构代码以使用更多函数后,出现了此错误。这是我到目前为止所得到的:

Java 代码

public class ModifyTimeController extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        PopulateTimeIntervals(request.getWriter());
    }
    protected void PopulateTimeIntervals(PrintWriter writer) {
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumFractionDigits(2);
        numberFormat.setMaximumFractionDigits(2);
        float workHours = (float)0.00;

        ...
        /* Code that queries a database for TimeIntervals */
        ...

        while(resultSet.next()) {
            // I was told that this type of conversion is
            // possible since Timestamp is an extension of Date
            Date dtStart = resultSet.getTimestamp("dtStart");
            Date dtEnd = resultSet.getTimestamp("dtEnd");

            // Accumulates the hours worked in each time interval
            workHours += CalculateWorkHours(dtStart, dtEnd);
        }
        // Should print out something like: 54.27
        writer.println(numberFormat.format(workHours).toString());
    }

    protected float CalculateWorkHours(Date dtStart, Date dtEnd) {
        // Divides the difference of the start and end times 
        // (in miliseconds) by 3600000 to convert to hours
        return (dtEnd.getTime() - dtStart.getTime()) / 3600000;
    }
}

这已经是漫长的一天了,所以我可能只是错过了一些东西......但是我没有打印出诸如 54.27 小时之类的东西,而是得到了一个固定 54 小时。之前数字格式工作得很好......所以我不知道发生了什么。

I've been glancing at this code for a while now, and I can't seem to figure out what the probably simple error is... In short, I have a float variable in Java that seems to only be storing the integer content (whole number) of what the value is actually supposed to be. I had this bit of code working before when I had everything crammed into one function, but after I re-factored the code to use more functions, this error occurred. Here's what I've got thus far:

Java Code

public class ModifyTimeController extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        PopulateTimeIntervals(request.getWriter());
    }
    protected void PopulateTimeIntervals(PrintWriter writer) {
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumFractionDigits(2);
        numberFormat.setMaximumFractionDigits(2);
        float workHours = (float)0.00;

        ...
        /* Code that queries a database for TimeIntervals */
        ...

        while(resultSet.next()) {
            // I was told that this type of conversion is
            // possible since Timestamp is an extension of Date
            Date dtStart = resultSet.getTimestamp("dtStart");
            Date dtEnd = resultSet.getTimestamp("dtEnd");

            // Accumulates the hours worked in each time interval
            workHours += CalculateWorkHours(dtStart, dtEnd);
        }
        // Should print out something like: 54.27
        writer.println(numberFormat.format(workHours).toString());
    }

    protected float CalculateWorkHours(Date dtStart, Date dtEnd) {
        // Divides the difference of the start and end times 
        // (in miliseconds) by 3600000 to convert to hours
        return (dtEnd.getTime() - dtStart.getTime()) / 3600000;
    }
}

It's been a long day, so I'm probably just missing something... But rather than printing out something like 54.27 hours, I'm getting a flat 54 hours. The number formatting worked just fine, before... So I don't know what's up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

脸赞 2024-12-12 18:51:45

return (dtEnd.getTime() - dtStart.getTime()) / 3600000; 处,您除以一个整数,并使结果成为一个整数。将其更改为 3600000.0 (或 3600000f),您应该会很满意。

At return (dtEnd.getTime() - dtStart.getTime()) / 3600000; you're dividing by an integer, and making the answer an integer. Change it to 3600000.0 (or 3600000f) and you should be golden.

老娘不死你永远是小三 2024-12-12 18:51:45
dtEnd.getTime() - dtStart.getTime()) / 3600000

这是 long 和 int 的除法,因此结果本身将是 long,然后转换为 float,然后仅保存计算出的 long 值。要获得浮点结果,请先将其中一个操作数强制转换为浮点,或使用浮点文字(如 3600000f)代替。

dtEnd.getTime() - dtStart.getTime()) / 3600000

This is a division of a long and int so the result itself will be a long and then be casted to float which then only holds the computed long value. To get a float result cast one of the operands to float first or use float literal like 3600000f instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文