使用 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(如预期,但没用)。 结果2:抛出IllegalArgumentException。
此时我已经没有选择下一步要尝试什么...
有什么建议吗?
I'm trying to do a pretty simple thing using Blackberry RIM API - I have a string 1000000
, that I want to format to 1,000,000.00
I have tried two RIM API classes in order to do that, but none of them did what I actually need:
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);
The result variable is 1000000.00
- it has decimal separator but is missing group separators (commas).
2) net.rim.device.api.i18n.MessageFormat (claims to be compatible with java.text.MessageFormat in Java's standard edition)
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: (when mfWithFormat
code commented out) gives me just a plain 1000000
(as expected, but useless).
result2: throws IllegalArgumentException
.
At this point I'm out of options what to try next...
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
http://supportforums.blackberry.com /t5/Java-Development/Format-a-decimal-number/mp/763981#M142257
Try this:
http://supportforums.blackberry.com/t5/Java-Development/Format-a-decimal-number/m-p/763981#M142257
很确定您必须编写自己的函数才能做到这一点。
Pretty sure you're going to have to write your own functions to do this.
这不需要创建您自己的函数:
确保传递整数类型而不是字符串,否则您将得到: java.lang.IllegalArgumentException: Cannot format给定的对象为数字
This works without the need of creating your own function:
Make sure you pass an integer type instead of a string otherwise you'll get: java.lang.IllegalArgumentException: Cannot format given Object as a Number