1. jmlowes's Avatar
    Hello all. I have written my first Blackberry app and it actually works! Anyway, I do need to pretty it up a little and have two question:

    1) The application generates output as strings. How can I format the strings so the output is in a dollars & cents format. For example $125.50 and not $125.5 or $125.4975? I could rip apart the string and test the value after the decimal and adjust accordingly. However, I am hoping there is something simple like printf("%1.2f", value) that can do this. If not, I will have to roll my own.

    2) I have a couple buttons in my app that I would like to have side by side. I understand that the default layout manager lays everything out vertically. How would I go about changing this for two buttons. The rest of the app is good layed out vertically.

    Any help would be greatly appreciated. Thanks.

    Posted from my CrackBerry at wapforums.crackberry.com

    Posted from my CrackBerry at wapforums.crackberry.com
    04-03-09 10:39 PM
  2. Mark Rejhon's Avatar
    1) The application generates output as strings. How can I format the strings so the output is in a dollars & cents format. For example $125.50 and not $125.5 or $125.4975? I could rip apart the string and test the value after the decimal and adjust accordingly. However, I am hoping there is something simple like printf("%1.2f", value) that can do this. If not, I will have to roll my own.

    2) I have a couple buttons in my app that I would like to have side by side. I understand that the default layout manager lays everything out vertically. How would I go about changing this for two buttons. The rest of the app is good layed out vertically.
    1) Unfortunately, there's no java.util.Formatter so you'll have to roll your own.
    When I print the cents in currency values in my apps, I just abs() it to positive, get only the decimal part (value % 1), multiply by 100, add 0.5 if necessary to help rounding to nearest integer, then (int) it to eliminate the rest of decimal digits (Because I pre-multiplied by 100, that gives me the 2-digit cents value as an integer), reconvert this integer to a string, then add a leading '0' if it's 1 character string instead of 2. Annoying series of steps, but works.

    2) Create a HorizontalFieldManager object and add your button fields to it. Then add the HorizontalFieldManager to your existing layout manager as if it was a single field. (i.e. treating the horizontal line of fields as a single field) Most of the layout managers are nestable within each other.
    04-04-09 12:04 AM
  3. Thyth's Avatar
    There is such a function in Java standard edition (PrintStream class, the method is format, and works pretty much the same way as the C standard I/O type).

    Unfortunately, it hasn't made it over to the BlackBerry PrintStream class.

    You might be able to see the implementation by looking at the classpath project, and copy it into your application.
    04-04-09 12:12 AM
  4. jmlowes's Avatar
    Thank you both for your responses. I did use printf when I wrote my app in Java SE and was hoping there was something as simple that I could employ. Oh well, I will copy mdrejhon's approach.

    Also, I will use the HorizontalFieldManager and add it to the default layout. I did not realize they could be nested like that, though it does make sense.

    Thanks again.

    Posted from my CrackBerry at wapforums.crackberry.com
    04-04-09 11:00 AM
LINK TO POST COPIED TO CLIPBOARD