Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 382
Registered: ‎05-19-2008
My Device: BlackBerry Torch
My Carrier: AT&T

Formatting a floating point number

I would think this should be very basic, but I just haven't has any success with it at all. I'm currently using Double.toString(d), but this option does not allow any formatting specifiers.  Ideally I would like it displayed ain a currency format i.e. ($4,50).

 

I first tried Format

Formatter fmt = new Formatter();

fmt.formatCurrency(d);

 

and later MessageFormat

 

Object[] Obj = {d};

MessageFormat.format("${0,number,###.##}",Obj)

 

In both of these cases, Eclipse would stop debugging. I assume it hit an exception, but I could not figure out how to see what the exception is. Eclipse is, well, it's no Visual Studio I guess is all I can say.

 

Soapbox aside, what is the suggested or prefered method for producing a FORMATTED string of a floating point number.

 

Similiarly, how is one supposed to round?Math has sin/cos/etc and no round?

 

Bill

Bill
-------------------------------------------
Check out my book on BlackBerry Development for Java.
And my other really really old book
My Apps: FlashKids
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Formatting a floating point number

I'm surprised that you are not seeing the Exception in Eclipse, but I'm not (yet) an Eclipse user, so I can't help you with that.  However I would recommend that you surround pieces of code like this with try/catch blocks, and then you can see the Exception being thrown.

 

In the case for Formatter, I suspect that the problem is the locale.  In my case, when I used the no parameter constructor, the current format failed with an UnsupportedLocaleException and the debug variables showed that the locale was "en-GB".  When  I used the 'null' constructor, I actually got some output, but without any currency symbol and with loads of digits (I had set the double to 123.56F and that seemed to represented internally as 123.55999755859375 which is what I got from the currency conversion).  

 

I added the following code:

          Locale [] locales =  Locale.getAvailableLocales();
          for ( int i = 0; i < locales.length; i++ ) {
              System.out.println(locales[i].toString());
          }

and got the following 'locales':


en
en_GB
en_US

(note the blank line was output).

 

So I tried again with various locales specified in the Formatter:

 

When I used Formatter("en"), I got: "$123.55"

 

However when I tried any of the others I got: UnsupportedLocaleException.

 

I don't understand this, so sorry don't have a solution, but thought you might be interested in my analysis.

 

This testing was done in JDE 4.6, using the default Bold simulator.

 

 

Developer
Posts: 382
Registered: ‎05-19-2008
My Device: BlackBerry Torch
My Carrier: AT&T

Re: Formatting a floating point number

Thanks Peter for the analysis. I didnt even try providing it some kind of local. It still seems to work poorly, so I'm hoping there is something else that I'm missing.

 

I'm only an eclipse user of - uh 2 days. I believe that you should leverage an existing product wherever possible and that RIM should focus on what they do best (which isnt making an IDE). But, having said that, the JDE does seem to have gotten better over the years and eclipse is still strange, but snapier. So, I'm not sure what direction is best and I'm still trying them both out.

 

Bill

Bill
-------------------------------------------
Check out my book on BlackBerry Development for Java.
And my other really really old book
My Apps: FlashKids
BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Formatting a floating point number

Currnetly only the "en" local is supported by the Formatter class.  I have sent feedback to our project management team to add support for additional locals.

 

The MesageFormatter class supports strings, dates and times.  It does not support numeric classes.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Developer
Posts: 86
Registered: ‎07-21-2009
My Device: Not Specified

Re: Formatting a floating point number

MessageFormatter doesn't support numeric types - SAD.

 

I have an app that I'm porting that does lots of calculations,

any tips, pointers, etc?

 

Would rather  not hand roll.

 

thanks!

lucidbee

Highlighted
Trusted Contributor
Posts: 227
Registered: ‎12-16-2009
My Device: 9530,8900,9000
My Carrier: Airtel,Docomo

Re: Formatting a floating point number

Hi MSoham,

Is there any other way to get the Currenncy?, i Checked it in os 5.0 still it supports only. "en","en_us" and "en_GB"..... so any other way to find the Currency?

Lathiya Himanshu