我想设置的入模式HALF_UP我DecimalFormat的,但是Eclipse是告诉我,setRoundingMode()是不可用的DecimalFormat的类。我的项目属性(和整体Eclipse的属性)所使用的1.6编译器。该developer.android网站说,我可以使用Java 5或6所以我不知道是什么问题。
I want to set the Rounding Mode to HALF_UP on my DecimalFormat, but eclipse is telling me that setRoundingMode() is not available on the DecimalFormat class. My project properties (and the overall Eclipse properties) are using the 1.6 compiler. The developer.android site says that I can use either Java 5 or 6 so I'm not sure what the problem is.
import java.math.RoundingMode; import java.text.DecimalFormat;
completedValueFormatter = NumberFormat.getNumberInstance(); DecimalFormat的completedDecimalFormat =(DecimalFormat的)completedValueFormatter; completedDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
completedValueFormatter = NumberFormat.getNumberInstance(); DecimalFormat completedDecimalFormat = (DecimalFormat)completedValueFormatter; completedDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
我也试着使用了android工具生成一只蚂蚁为基础的项目,尝试这个code项目,也得到了相同的编译错误。因此,它似乎并没有涉及到月食。这似乎与Android的API。
I've also tried using the android tools to generate an ant-based project, tried this code in the project and also got the same compile error. So it doesn't appear to be related to Eclipse. It seems related to the Android API.
有什么建议?
推荐答案这并不能真正回答为什么我不能用在DecimalFormat的Java 6的.setRoundingMode(的RoundingMode)方法,但它至少是一个变通
This doesn't truly answer why I can't use the Java 6 .setRoundingMode(RoundingMode) method in DecimalFormat, but it is at least a work-around.
int numDigitsToShow = thispletedValueFormatter.getMaximumFractionDigits(); BigDecimal bigDecimal = new BigDecimal(valueToBeRounded); BigDecimal roundedBigDecimal = bigDecimal.setScale(numDigitsToShow, RoundingMode.HALF_UP); return thispletedValueFormatter.format(roundedBigDecimal.doubleValue());
我创建一个BigDecimal与我需要四舍五入的值,然后我得到的规模设置为数字,我需要我的舍入值与数字值的一个BigDecimal。然后,我通过圆润的价值关我转换为字符串原始的NumberFormat。
I create a BigDecimal with the value I need to round, then I get a BigDecimal of that value with the scale set to the number of digits I need to round my values to. Then I pass that rounded value off to my original NumberFormat for conversion to String.
如果任何人有一个更好的解决方案,我所有的耳朵!
If anyone has a better solution, I'm all ears!
更多推荐
四舍五入半了在Android的十进制格式
发布评论