java - 当 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;
}
}
解决方案
不确定我是否正确理解了您的问题,但我认为您正在尝试打印没有十进制值的双精度值。
您可以通过执行以下操作将 double 值转换为 int 值:int x = (int) y
here y
is double。现在,如果您打印x
,那么您将不会得到任何小数位。
注意: int 类型转换不是一个好主意,因为您的 double 可能超出范围。
推荐阅读
- python - Ranking words across multiple text files by TFIDF
- android - 'Received status code 400 from server: Bad Request' While publishing aar file to Github packages
- html - 如何在第二部分html css的顶部添加部分?
- javascript - CoreUI icons from npm not showing in ReactJS project
- python-3.x - PySpark:如何为所有列转换字符串数据类型
- php - "Delivered Message to APNS" but not showing notification in Production with ios13 (PHP)
- wordpress - 如何在我的自定义插件之前加载 WooCommerce
- r - 将向量元素分配给同一向量中不是自身的另一个元素(秘密圣诞老人算法)
- sql - 如何找到相等的子集?
- javascript - 如何根据javascript中的特定字符序列拆分文本