四舍五入半了在Android的十进制格式

编程入门 行业动态 更新时间:2024-10-17 23:36:56
本文介绍了四舍五入半了在Android的十进制格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想设置的入模式H​​ALF_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的十进制格式

本文发布于:2023-11-10 02:44:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574123.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:四舍五入   格式   Android   十进制

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!