Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alignment of decimals, remove trailing zeros but keep padding 3

Status
Not open for further replies.

dawnyanne

Technical User
Nov 1, 2006
34
GB
Hi

I wonder if anyone can help - I have searched through all the posts and cant find my specific answer.

I need numbers to display decimal places if there are any and if there isnt - to not show zeros but pad the spacing out so that the decimals line up. e.g

1000.00 = 1000
24.123 = 24.123
6.10 = 6.10
247893.00 = 247893
16.1234 = 16.1234

I am using crystal version 8.5.
I have used other formulas on the site - which does get rid of the trailing zeros - but then all of the numbers align to the right.

If anyone can help - I would be hugely grateful! I seem to be having a bit of a 'blonde' moment with this!
kind regards
dawnyanne
 
One method is to have two separate fields, placed next to each other on the page. Use Truncate to show numbers without decimal. Subtract the truncated value from the original to get the decimals.

[yinyang] Madawc Williams (East Anglia, UK). Using Windows XP & Crystal 10 [yinyang]
 
You'll probably need to know the longest integer and longest number of decimals before you start printing these numbers. Only if you have these two facts could you use a formula to make all numbers align nicely, and you must make sure to use a single spaced font type.

Perhaps a subreport before displaying the numbers, in which you find the longest integer and number of decimals (find these by using a split on the decimal seperator and then count the lenght of the field (use a totext before reading the field in the split function)).

When you have the facts you could test each field and supplement the missing integers or decimals with spaces (depending on left or right text alignment)
 
The following works:

stringvar x := totext({@number},8,"");
numbervar pos := instr(x,".");
stringvar y := "";

y :=
space(8 - len(left(x, pos-1))) + left(x, pos)
+
mid(totext(val(mid(x,pos)),
(
if val(right(x,8)) = 0 then 0 else
if val(right(x,7)) = 0 then 1 else
if val(right(x,6)) = 0 then 2 else
if val(right(x,5)) = 0 then 3 else
if val(right(x,4)) = 0 then 4 else
if val(right(x,3)) = 0 then 5 else
if val(right(x,2)) = 0 then 6 else
if val(right(x,1)) = 0 then 7 else
8
), ""),3);
if
right(y,1) = "." then
left(y,len(y)-1) else
y

This is set up to allow for 8 digits to the left of the decimal and 8 to the right. Adjust as necessary.

You must format the formula to a nonproportional font like Courier New for this to work.

-LB
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top