08-22-2009 08:53 PM
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
08-23-2009 04:43 AM
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.
08-23-2009 11:01 PM
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
08-25-2009 03:11 PM
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.
10-09-2009 02:53 PM
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
05-25-2010 09:18 AM
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?