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
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

How to format as a percent?

Where is the proper import library for the java function to set decimal places and percent signs for a number?  I have googled this like crazy and cannot find where in the BB libraries it is.

 

Any sample code for formatting a number as a percent and setting decimal places greatly appreciated.

 

Thanks.

 

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Please use plain text.
Developer
JCarty
Posts: 1,055
Registered: ‎01-25-2009
My Device: BlackBerry Bold 9650/BlackBerry Torch 9800

Re: How to format as a percent?

Scroll down a little on the following link and you should find what you want to accomplish:

 

http://java.sun.com/j2se/1.3/docs/api/java/text/MessageFormat.html

Jerome Carty - Follow jcarty on Twitter@jcarty | #webworks-dev / #BlackberryDev on irc.freenode.net | My Apps
Click "Accept as Solution" if post solved your original issue.. Give like/thumbs up if you feel post is helpful
Please use plain text.
Developer
chengbang69
Posts: 62
Registered: ‎01-22-2010
My Device: BOLD 9700
My Carrier: Globe

Re: How to format as a percent?

you can try RIM's Formatter package, it has a method formatPercentage.

http://www.blackberry.com/developers/docs/4.3.0api/javax/microedition/global/Formatter.html

 

formatPercentage(float value, int decimals)
          Formats a percentage with the specified number of decimals using locale-specific rules.

 

formatPercentage(long value)
          Formats an integral percentage value using locale-specific rules.

 

 

Please use plain text.
Developer
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

Re: How to format as a percent?

            	float annualdollarcoupon = (java.lang.Float.parseFloat(_efAnnualCoupon.getText()));
            	float currentprice = (java.lang.Float.parseFloat(_efPrice.getText()));
            	
            	javax.microedition.global.Formatter f = new javax.microedition.global.Formatter();
            	_lfResults.setText(f.formatPercentage((CalcLib.CurrentYield(annualdollarcoupon, currentprice)),3));

 

 

When I try the above, I get an 'unsupported locale exception' and the app takes a dump.

 

Thanks for your help.  What else could I be missing?

 

 

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Please use plain text.
Developer
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

Re: How to format as a percent?

I think I fountd it. 

 

My simulator had English-UK for a language.  I changed to English US and it worked.

 

This begs a new question.  How can I better handle this exception, or ensure all locales are supported?

 

If I published the app globally, I do not want it to break.

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Please use plain text.