Crystal Reports Training by Ken Hamady, MS, Reporting and Training Nationwide On Location TrainingPublic ClassesIndividual TrainingIntroductory Course OutlineAdvanced Course OutlineFormula ExamplesNewsletter Back IssuesMy BlogResource LibraryConsulting ServicesSupport ServicesContact InformationLinks to third party productsBack to main pageMy Credentials
Free Crystal Reports formula examples from KenHamady.com


To learn the techniques used in these formulas get:


The Expert's Guide to Crystal Reports Formulas
and
 Expert Techniques for Crystal Reports I, II & III

Convert Decimal Numbers to Text showing only the non-zero decimals:

Whenever you display a column of numbers you have to decide how many decimal places to display, and CR will round the rest up or down as needed.  When you convert to text using the ToText function you also have to decide how many decimals to display.  But lets say you have the following values.  In both cases all of the numbers will have the same number of decimals, and will use zeros to fill out to the right like this:
 
1.0500
1.4050
1.2345
But say you want the values above to appear this way when converted to text.
1.05
1.405
1.2345
In other words, you don't want any rounding, and you don't want trailing zeros after the decimal.   The following formula will do the trick as long as you don't have any numbers with over 6 decimal places.  If you want to allow more than 6 decimial places, you can expand the pattern below to add more:
WhileReadingRecords ;
StringVar text     :=  Totext ( {Your.NumberField} , 6 , ""  )  ;  //put your numeric field in this line
NumberVar end  :=  length ( text ) ;
NumberVar clip  :=
(if  Val ( text [ end - 6 to end ] ) = 0 then 1 else 0 ) +
(if  Val ( text [ end - 5 to end ] ) = 0 then 1 else 0 ) +
(if  Val ( text [ end - 4 to end ] ) = 0 then 1 else 0 ) +
(if  Val ( text [ end - 3 to end ] ) = 0 then 1 else 0 ) +
(if  Val ( text [ end - 2 to end ] ) = 0 then 1 else 0 ) +
(if  Val ( text [ end - 1 to end ] ) = 0 then 1 else 0 ) +
(if  Val ( text [ end - 0 to end ] ) = 0 then 1 else 0 )  ;
text [ 1 to Length ( text ) - clip ]