Android 中以小数格式四舍五入一半
我想在 DecimalFormat 上将舍入模式设置为 HALF_UP,但 eclipse 告诉我 setRoundingMode() 在 DecimalFormat 类上不可用。我的项目属性(以及整个 Eclipse 属性)使用的是 1.6 编译器。 developer.android.com 网站说我可以使用 Java 5 或 6,所以我不确定问题是什么。
import java.math.RoundingMode;
import java.text.DecimalFormat;
CompletedValueFormatter = NumberFormat.getNumberInstance();
DecimalFormat完成DecimalFormat = (DecimalFormat)completedValueFormatter;
CompletedDecimalFormat.setRoundingMode(RoundingMode.HALF_UP);
我还尝试过使用android工具生成一个基于ant的项目,在项目中尝试了这段代码,也得到了相同的编译错误。所以它似乎与Eclipse无关。看来和Android API有关。
有什么建议吗?
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.com 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);
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.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这并不能真正回答为什么我不能在 DecimalFormat 中使用 Java 6 .setRoundingMode(RoundingMode) 方法,但它至少是一个解决方法。
我使用需要舍入的值创建一个 BigDecimal,然后得到该值的 BigDecimal,其比例设置为我需要将值舍入到的位数。然后我将舍入值传递给原始 NumberFormat 以转换为字符串。
如果有人有更好的解决方案,我洗耳恭听!
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.
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!
这是我怀疑的问题所在(假设我正确阅读了文档)并且它很奇怪:
根据 java.text.DecimalFormat API 文档,您实际上并没有获得 Java 1.6 RE 的运行时实现,而是得到一个android“增强版”,显然不包括setRoundingMode,坦率地说,它很糟糕。
“这是 DecimalFormat 的增强版本,基于 RI 中的标准版本。新的或更改的功能被标记为 NEW。”
多年来,Java 中的一个弱点是 DecimalFormat 类默认为 HALF_ROUND_UP 并且无法更改它,直到 JVM 1.6。遗憾的是,Android 仍然保留着这种拼凑的需求。
因此,看起来我们陷入了在任何需要它的应用程序中拼凑 BigDecimal 比例设置来格式化输出的困境,而不是仅仅依靠格式化程序调用来完成工作。不是世界末日,而是谷歌非常令人失望。
当然,同一个文档说 setRondingMode() 有效,所以也许这是一个全面的 BUG?
Here is what I suspect the problem is, (assuming I am reading the docs properly) and its a doozy:
According to the java.text.DecimalFormat API documentation, you are not actually getting the Runtime Implimentation of the Java 1.6 RE, but are getting an android "Enhanced Version" that clearly doesn't include the setRoundingMode, which frankly bites.
"This is an enhanced version of DecimalFormat that is based on the standard version in the RI. New or changed functionality is labeled NEW."
A weakness in Java for many many many years has been the DecimalFormat class defaulted to HALF_ROUND_UP and had no way to change that, until JVM 1.6. Pity to see Android is keeping this need to kludge alive.
So looks like we are stuck Kludging BigDecimal scale Settings to format output all over any app that needs it, instead of simply being able to rely on a formatter call alone to get the job done. Not the end of the world, but very disappointing Google.
Of course that same doc says that setRondingMode() works, so perhaps this is a all out BUG??
我想这将是最好的选择
http://download.oracle .com/javase/1.4.2/docs/api/java/lang/Math.html#ceil(double)
I guess this would be the best option
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html#ceil(double)