我正在尝试使用黑莓 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
(正如预期的那样,但没有用)。
结果 2:抛出 IllegalArgumentException
。
此时我无法选择下一步要尝试什么...
有什么建议吗?
最佳答案
试试这个: http://supportforums.blackberry.com/t5/Java-Development/Format-a-decimal-number/m-p/763981#M142257
关于java - 使用 Blackberry Java API 将十进制数格式化为 (###,###.##),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4833597/