首页 > 解决方案 > 当 double 作为 int 输入时,尝试输出不带小数的 double

问题描述

每当为我的双精度输入一个 int 时,我都会尝试打印出一个 int。例如用户输入 123456,输出 123456;用户输入 1.0,输出 1.0。截至目前,我的代码无论如何都会打印一个双精度。这适用于我的 fullCellText 方法。

我的代码:

package textExcel;

public class ValueCell extends RealCell {
    private boolean isInt;
    public ValueCell(String cell) { 
        super(cell);
        // TODO Auto-generated constructor stub
    }
    public ValueCell(String cell, boolean isInt) { 
        super(cell);
        this.isInt = isInt;
        // TODO Auto-generated constructor stub
    }
    public String fullCellText() {
        return "" + cell;
    }

}

标签: javaintdouble

解决方案


不确定我是否正确理解了您的问题,但我认为您正在尝试打印没有十进制值的双精度值。

您可以通过执行以下操作将 double 值转换为 int 值:int x = (int) yhere yis double。现在,如果您打印x,那么您将不会得到任何小数位。

注意: int 类型转换不是一个好主意,因为您的 double 可能超出范围。


推荐阅读