使用Blackberry Java API将十进制数格式化为(###,###.##)

我正在尝试使用Blackberry RIM API做一个非常简单的事情 – 我有一个字符串1000000,我要格式化为1,000,000.00

为了做到这一点,我尝试了两个RIM API类,但没有一个做我真正需要的:

1)javax.microedition.global.Formatter

String value = "1000000";  
float floatValue = Float.parseFloat(value);  
Formatter f = new Formatter(); //also tried with locale specified - Formatter("en")  
String result = f.formatNumber(floatValue, 2);

结果变量是1000000.00 – 它有小数点分隔符但缺少组分隔符(逗号).

2)net.rim.device.api.i18n.MessageFormat(声称与Java标准版中的java.text.MessageFormat兼容)

 String value = "1000000";  
 Object[] objs = {value};  
 MessageFormat mfPlain = new MessageFormat("{0}");  
 MessageFormat mfWithFormat = new MessageFormat("{0,number,###,###.##}");  
 String result1 = mfPlain.format(objs);  
 String result2 = mfWithFormat.format(objs);  

result1 :(当mfWithFormat代码被注释掉时)给了我一个普通的1000000(正如预期的那样,但没用).
result2:抛出IllegalArgumentException.

在这一点上,我没有选择接下来要尝试什么…

有什么建议?

最佳答案 试试这个:

http://supportforums.blackberry.com/t5/Java-Development/Format-a-decimal-number/m-p/763981#M142257

点赞