Java 在TextView中设置数字的自定义格式

Java 在TextView中设置数字的自定义格式,java,android,format,comma,settext,Java,Android,Format,Comma,Settext,可能重复: 我希望能够从一个双变量(称为double)获取输出,并将其与一些字符串文本(称为outputPref,仅包含“cubic mm”)一起放入TextView,如下所示 displayAnswer.setText(volume + " " + outputPref); 这很好,但是,这个数字在TextView中显示为完整的双精度(有很多很多小数位)。我想用逗号将数千(如果有的话)除以一个小数位来显示数字,比如Excel数字格式“(*#,##0.0);(*(#,##0.0);(*”-

可能重复:

我希望能够从一个双变量(称为double)获取输出,并将其与一些字符串文本(称为outputPref,仅包含“cubic mm”)一起放入TextView,如下所示

displayAnswer.setText(volume + " " + outputPref);
这很好,但是,这个数字在TextView中显示为完整的双精度(有很多很多小数位)。我想用逗号将数千(如果有的话)除以一个小数位来显示数字,比如Excel数字格式“(*#,##0.0);(*(#,##0.0);(*”-“?)(@#)”,或者简单地说(假设我没有任何负数) “#,##0.0”


在将volume double变量应用于TextView之前,我应该使用什么函数来重新配置它?

您可以将double数字四舍五入以在TextView中显示:

double roundTwoDecimals(double d) {
            DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
}

您可以在文本视图中显示双倍数字:

double roundTwoDecimals(double d) {
            DecimalFormat twoDForm = new DecimalFormat("#.##");
        return Double.valueOf(twoDForm.format(d));
}
用这个

NumberFormat format = NumberFormat.getInstance();
        format.setMaximumFractionDigits(1);
displayAnswer.setText(format.format(volume)+ " "+outputPref);
用这个

NumberFormat format = NumberFormat.getInstance();
        format.setMaximumFractionDigits(1);
displayAnswer.setText(format.format(volume)+ " "+outputPref);

您可以使用以下选项设置小数位数:

DecimalFormat df = new DecimalFormat("#.#");
这将在整数后给出一个小数位,因为小数位后有一个#符号

然后做:

df.format(yourDoubleHere);

您可以使用以下选项设置小数位数:

DecimalFormat df = new DecimalFormat("#.#");
这将在整数后给出一个小数位,因为小数位后有一个#符号

然后做:

df.format(yourDoubleHere);

我希望它能帮助你

rountdoubel=Maths.round(outputPref)

displayAnswer.setText(volume + " " + rountdoubel);

我希望它能帮助你

rountdoubel=Maths.round(outputPref)

displayAnswer.setText(volume + " " + rountdoubel);

太好了。如果我想用逗号来分隔千分之二->也就是说,不要用整数小数()把789455.566变成789455.57,它就会变成789455.57。太好了。如果我想用逗号来分隔千分之二->也就是说,不要用整数小数()怎么办把789455.566变成789455.57它会变成789455.57吗?