BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-31-2008, 10:45 AM   #1 (permalink)
Knows Where the Search Button Is
 
Join Date: Mar 2008
Model: 8820
PIN: N/A
Carrier: at&t
Posts: 40
Post Thanks: 0
Thanked 0 Times in 0 Posts
Question decimal aligning

Please Login to Remove!

One thing I've been trying to figure out is how to decimal align amounts, and limit them to two decimals. Was trying to vertically align text fields, but am having problems discovering how to do this in Java.
Offline  
Old 07-31-2008, 11:58 PM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Dec 2005
Model: 8310
Carrier: Rogers
Posts: 138
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Could you please provide a visual example of what you mean?
Offline  
Old 08-01-2008, 11:51 AM   #3 (permalink)
Knows Where the Search Button Is
 
Join Date: Mar 2008
Model: 8820
PIN: N/A
Carrier: at&t
Posts: 40
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by CELITE View Post
Could you please provide a visual example of what you mean?
Sure. Say you want to display something like this with editable fields:

Code:
Cost:  $ 100.00
Tax:         6%
Total: $ 106.00
Offline  
Old 08-02-2008, 03:10 PM   #4 (permalink)
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 8310
PIN: N/A
Carrier: ATT
Posts: 17
Post Thanks: 0
Thanked 0 Times in 0 Posts
Arrow

Blackberry doesn't have format utilities ( yet). Any formating, you need to do yourself. Below is soem quick and dirty code on how to format currency to 2 decimal places.


Code:
        double currency = 1.324323;
        String formated = "$" + String.valueOf((double)(int)((currency+0.005)*100.0)/100.0);
To align, you would need to look at the length of the string and pad it with spaces. Hope this helps
Offline  
Old 08-02-2008, 03:46 PM   #5 (permalink)
Knows Where the Search Button Is
 
Join Date: Mar 2008
Model: 8820
PIN: N/A
Carrier: at&t
Posts: 40
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

That helps (actually had gotten that far), but the real problem is aligning to the decimal. But I guess that really is just a matter of right justifying and laying out the fields vertically algined with the same width.

Just wanted to see what others would say.
Offline  
Old 08-04-2008, 10:07 AM   #6 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

The alignment issue is generally a function of the type of font you use, you'll need a fixed width font. not sure which of the shipped bb fonts are fixed width but there should be some documentation on that out there.
Offline  
Old 08-04-2008, 12:30 PM   #7 (permalink)
Knows Where the Search Button Is
 
Join Date: Mar 2008
Model: 8820
PIN: N/A
Carrier: at&t
Posts: 40
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by bemshaswing View Post
The alignment issue is generally a function of the type of font you use, you'll need a fixed width font. not sure which of the shipped bb fonts are fixed width but there should be some documentation on that out there.
Has nothing to do with font if you are using the same font. Who would use different fonts????

Think of the problem this way... say you have three numbers you want to vertically align on the decimal, but one has 2 decimal places, one, 1, and the last 3. Moreover, you are not wanting to pad them with zeros.

Anyway, it's not the important, since I'm using fixed two decimals, it's easy.
Offline  
Old 08-04-2008, 01:10 PM   #8 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

i'm talking about the same font. in a fixed with font the width of say 1 and 0 would be the same. not the case in a non-fixed width font.
Offline  
Old 08-04-2008, 08:02 PM   #9 (permalink)
Knows Where the Search Button Is
 
Join Date: May 2008
Model: 8310
PIN: N/A
Carrier: ATT
Posts: 17
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Even with non-fixed width, you can find out the required width using
Code:
Font.getAdvance(...)
method. But not sure if its worth all this trouble.
Offline  
Old 08-08-2008, 01:53 PM   #10 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2006
Location: Boston
Model: 8900
Carrier: AT&T
Posts: 98
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi!

Fonts are always an exciting game to play. And no, I'm not just kidding.

Fixed-width fonts (think Courier) allot the same space for each character - the space for "$ 100.00" is identical to the space for "$ 888.88" is identical to the space for "W WWW.WW". Non-fixed width fonts (think Times-Roman) allot different space for different characters - the space for "i i" is different from the space for "w w".

You can find the width of a string for any font. However, to align to a particular character within a string becomes more challenging if you're using a non-fixed-width font. The alignment shown in pwaugh's second entry above is not simply aligning the decimal points - the digits are also aligned, with the "hundreds" digit in one line aligned up with its corresponding digit in subsequent lines. You would need to use Font.getAdvance() for each digit and the decimal point in the string to achieve this alignment, and the end result would be fixing the width of a non-fixed-width font (you would end up forcing each character to be drawn in the same space occupied by an "8" or a "0", whichever is the largest-width). This may look ugly - you could right- or left- or center-justify each character, but that tends to make the resulting display look "weird".

Your best bet is to force the font to be fixed-width instead (i.e., create a Font object from a fixed-width font and assign it). The only issue I can envision with doing it this way is finding a fixed-width font on the device without knowing whether one exists. I believe that every BlackBerry comes with at least one by default, but I'm not sure how to track it down programmatically.

Good luck!

Cheers,

karl
__________________
Karl G. Kowalski
---------------
Owns a RAZR
Develops for BlackBerry
So next phone will be........an iPhone 3G!
Offline  
Old 08-08-2008, 04:01 PM   #11 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Oct 2006
Model: 7103
Carrier: Verizon
Posts: 259
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

yes, holydaps speaks the truth.
regarding "but I'm not sure how to track it down programmatically."
FontFamily.getFontFamilies() will return you an array that you can select from. you do have to know what you're looking for (e.g. BBCondensed, Courier, etc)
Offline  
Old 08-09-2008, 09:18 PM   #12 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2006
Location: Boston
Model: 8900
Carrier: AT&T
Posts: 98
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the info bemshaswing - it's been a long while since I was "font-fishing". My next step would probably be to create a test app that just shows all the available fonts on a device, and run it on several simulators to see if there was a consistent pattern to search for - something like String.toLowerCase().indexOf( "courier" ) or the equivalent. I'd also want a failsafe algorithm, which might prove challenging and would probably be a waste of time.

Cheers,

karl
__________________
Karl G. Kowalski
---------------
Owns a RAZR
Develops for BlackBerry
So next phone will be........an iPhone 3G!
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.