开发者

Format a decimal number to (###,###.##) using Blackberry Java API

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 = mf开发者_Python百科WithFormat.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?


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.


This works without the need of creating your own function:

String value = "1000000";

MessageFormat msgFormat = new MessageFormat("{0,number,###,###.00}");
String s = msgFormat.format(new Object[]{Integer.valueOf(value)}));

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜