Format decimal values on a BlackBerry

痴心易碎 提交于 2019-12-13 03:39:13

问题


I am displaying a double to the user, but it is printed as 1.00000000001

However, I need only two digits after the decimal point.


回答1:


There's a class called Formatter that can do the trick. Here's a code snippet:

double value = 1.24790000001;
Formatter formatter = new Formatter();
String formatted = formatter.formatNumber(value, 2);

And, here's a link to the JavaDoc: javax.microedition.global.Formatter




回答2:


Have you looked at String.format e.g.

String x = String.format("%.2f", number);

http://download.oracle.com/javase/6/docs/api/java/lang/String.html



来源:https://stackoverflow.com/questions/4031625/format-decimal-values-on-a-blackberry

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!