Java 如何删除小数点后出现的零
为了加法,我声明了两个浮点变量 因此,用户可以输入2.2+2.2,得到4.4 有时,用户会输入2+2并获得4.0>>此零需要删除 我不能使用舍入法,也不能将整个结果转换为整数 我的实际代码如下:Java 如何删除小数点后出现的零,java,android,precision,Java,Android,Precision,为了加法,我声明了两个浮点变量 因此,用户可以输入2.2+2.2,得到4.4 有时,用户会输入2+2并获得4.0>>此零需要删除 我不能使用舍入法,也不能将整个结果转换为整数 我的实际代码如下: holder.txtTotalViews.setText(new StringBuilder().append(new java.text.DecimalFormat("#").format(Float.parseFloat(trendingVideoList.get(position).getTota
holder.txtTotalViews.setText(new StringBuilder().append(new java.text.DecimalFormat("#").format(Float.parseFloat(trendingVideoList.get(position).getTotal_views())/1000)).append("K ").append(activity.getString(R.string.lbl_views)));
我需要将视频的总视图计算为K格式,如10K和10.5K。
我无法从10.0K中删除零。我的问题通过更改代码的十进制格式得到解决: 感谢@Jyoti now我的代码: 我需要像这样将我的
新java.text.DecimalFormat(“#”)更改为新java.text.DecimalFormat(“#.#”)
holder.txtTotalViews.setText(new StringBuilder().append(new java.text.DecimalFormat("#.#").format(Float.parseFloat(trendingVideoList.get(position).getTotal_views())/1000)).append("K ").append(activity.getString(R.string.lbl_views)));
我的问题通过更改代码的十进制格式得到解决:
感谢@Jyoti now我的代码:
我需要像这样将我的新java.text.DecimalFormat(“#”)更改为新java.text.DecimalFormat(“#.#”)
holder.txtTotalViews.setText(new StringBuilder().append(new java.text.DecimalFormat("#.#").format(Float.parseFloat(trendingVideoList.get(position).getTotal_views())/1000)).append("K ").append(activity.getString(R.string.lbl_views)));
您可以创建如下常见函数:
// No need to re-create decimal format instance. Keep this outside of the function.
DecimalFormat decimalFormat = new DecimalFormat("#.#");
public String numberToString(float number){
if(number == (int) number){
return Integer.toString((int) number);
}
return decimalFormat.format(number);
}
您可以创建如下常见函数:
// No need to re-create decimal format instance. Keep this outside of the function.
DecimalFormat decimalFormat = new DecimalFormat("#.#");
public String numberToString(float number){
if(number == (int) number){
return Integer.toString((int) number);
}
return decimalFormat.format(number);
}
使用DecimalFormat(“#.#”)。我需要将视频的总视图计算为K格式,如10K和10.5K。我无法从10.0K中删除零。请使用DecimalFormat(“#.#”)。格式(解决方案)
此问题的可能重复项无法解决我的问题。我需要将视频的总视图计算为K格式,如10K和10.5K。我无法从10.0K中删除零。