[英]How to remove the decimal part if it is only 0?
There is a calculation : Double ret = Double.parseDouble(v.getCible_1()) - Double.parseDouble(v.getRealise_1());
有一个计算: Double ret = Double.parseDouble(v.getCible_1()) - Double.parseDouble(v.getRealise_1());
If ret = 68.0
how to get only ret = 68
?如果ret = 68.0
如何只得到ret = 68
?
EDIT : I want to keep decimal part if it is not 0 , for example ret = 68.4 will be ok.编辑:如果不是 0 ,我想保留小数部分,例如 ret = 68.4 就可以了。
You can write like this.你可以这样写。
double doubleValue = 68.1;
int intValue = (int)doubleValue;
double minus = doubleValue-intValue;
if(minus != 0.0){
System.out.println(doubleValue);
}
else{
System.out.println(intValue);
}
Use DecimalFormat Class to format numbers.使用 DecimalFormat 类来格式化数字。
https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html
You can convert it to int .. like this您可以将其转换为 int .. 像这样
int value = (int) 68.0;
it will give you 68它会给你 68
You can cast it to an int
and compare it with the double
value.您可以将其转换为int
并将其与double
值进行比较。 If the comparison results in true
then you have to use the converted value or else continue using the double
.如果比较结果为true
则您必须使用转换后的值,否则继续使用double
。
Do something like this,做这样的事情,
private String getResultValue(double ret) {
int convertedValue = (int) ret;
return String.valueOf(ret == convertedValue ? convertedValue : ret);
}
For Example,例如,
When ret
= 68.0, convertedValue
= 68 --> Comparison: True
, Returned Value: 68当ret
= 68.0, convertedValue
= 68 -->比较: True
,返回值: 68
When ret
= 68.4, convertedValue
= 68 --> Comparison: False
, Returned Value: 68.4当ret
= 68.4, convertedValue
= 68 -->比较: False
,返回值: 68.4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.