开发者

Format decimal values on a BlackBerry

开发者 https://www.devze.com 2023-01-22 17:45 出处:网络
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.There\'s a class called Formatter that can do the tri

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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消