上的双值
问题描述:
以下代码上的双值
private double roundTheReading(double toRoundValue) {
double roundetValue = 0;
if (formatter == null){
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
formatter = new DecimalFormat(("#0." + zerosAfterDot),dfs);
}
roundetValue = Double.parseDouble(formatter.format(toRoundValue));
return roundetValue;
}
否pointation使得双“toRoundValue”的短路,因为它富人9后点的字符和我需要仅0,1或它们(2被设置通过输入法输入零点后)。
问题是:如果zerosAfterDot为“0”,答案应该是5或3421。每次1零(5.0 3421.0)。
剂量这有什么做的线:
formatter = new DecimalFormat(("#0." + zerosAfterDot),dfs);
是在这条线的点的点我在输出得到什么?如果是这样,我可以使If()检查点的值是否为零。但我宁愿不这样做,因为如果零后零> 0,它也会杀死点。我想绕过许多if语句。
TL,DR我该如何摆脱“。”当我没有数字后面那个重要?
答
您可能正在显示从roundTheReading
返回的double
值,该值始终包含非有效数字。相反,你可以返回一个String
和使用的
formatter = new DecimalFormat(("0.#");
+0
该值用于其他方式,因此字符串返回不是最好的方式,因为每次我想要使用时都需要将其返回。它。但我可以在UI中给getter方法中的字符串。它只是一个简单的答案,我没有想到,直到我看到你的。感谢那! – Fulli 2014-09-05 10:23:28
可能重复[如何很好地格式化浮点数为String避免不必要的小数0?(http://*.com/questions/703396/how-to-nicely -format-floating-numbers-to-string-without-unnecessary-decimal-0) – icza 2014-09-05 10:11:17