Question

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?

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top