views:

192

answers:

2

I have a need to display a currency value in my application. There doesn't seem to be an easy way to do this with the RIM API, so I'm reduced to creating my own solution (a common refrain for BlackBerry development, unfortunately)

Currently I'm using the Formatter class, from javax.microedition.locale like so

protected String formatResult(double result)
{
  try {
     Locale l = Locale.getDefaultForSystem();
     Formatter formatter = new Formatter(l.toString());
     return formatter.formatCurrency(result);
  } catch (UnsupportedLocaleException e)
  {
     return "This fails for the default locale because BlackBerry sucks";
  }      
}

I always hit the catch block in the simulator. Since this doesn't work by default on the simulator, I'm hesitant to put it in the application.

Can anyone tell me if the above solution is the way to go? And how to fix it, of course.

+1  A: 

From javax.microedition.global.Formatter javadoc at Blackberry.com:

This implementation of the Formatter class supports only locale-neutral formatting at this time.

Also, a developer at BB support forum mentions the following in this topic:

Currnetly only the "en" local is supported by the Formatter class.

So, you're pretty lost I think.

BalusC
+1  A: 

BalusC is correct, currently the only supported locale is "en". A slight tweak of your example should work (at least for the "en" locale ;) ):

protected String formatResult(double result)
{
  try {
     Formatter formatter = new Formatter("en");
     return formatter.formatCurrency(result);
  } catch (UnsupportedLocaleException e)
  {
     return "This fails for the default locale because BlackBerry sucks";
  }      
}

The API documentation states: "The result uses the locale-specific decimal separator and may include grouping separators." In practice grouping separators are not included for me (I don't get any commas for US dollar amounts >= 1000).

cmour
That might work for me, thanks. Do you know if the en locale is always installed on all BlackBerrys, regardless of location?
ageektrapped