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

I am trying to do a fairly simple thing using the Blackberry RIM API. I have a string of 1000000 that I want to format to 1,000,000.00

I tried two RIM API classes to do this, but none of them did what I really needed:

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 resulting variable is 1000000.00 - it has a decimal separator, but there are no group separators (commas).

2) net.rim.device.api.i18n.MessageFormat (claims to be compatible with java.text.MessageFormat in the standard version of Java)

  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 the mfWithFormat code mfWithFormat commented out) it gives me just the usual 1000000 (as expected, but useless). result2: throws IllegalArgumentException .

At this moment, I have no options what to try next ...

Any suggestions?

+4
source share
3 answers

Pretty sure you will need to write your own functions for this.

0
source

This works without having to create 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 will get: java.lang.IllegalArgumentException: it is not possible to format this object as a number

-1
source

Source: https://habr.com/ru/post/1337552/


All Articles