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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditionally format number of decimals dispayed

Status
Not open for further replies.

Crystalyzer

Technical User
Mar 28, 2003
218
Hi all,

I want to control the number of decimals displayed in my number format. If there are 3 decimals I want to dispay the number as "1.234" and if there are 0 decimals I want to display it as "1". I want this for up to 5 decimal places.

I have entered the following formula in the custom style decimal format formula box and the default format is set to 5 decimals and rounded to 5 places.

Code:
If Truncate(CurrentFieldValue)- ROUND(CurrentFieldValue,6) = 0 then 0 Else
If Truncate(CurrentFieldValue,1)- ROUND(CurrentFieldValue,6) = 0 then 1 Else
If Truncate(CurrentFieldValue,2)- ROUND(CurrentFieldValue,6) = 0 then 2 Else
If Truncate(CurrentFieldValue,3)- ROUND(CurrentFieldValue,6) = 0 then 3 Else
If Truncate(CurrentFieldValue,4)- ROUND(CurrentFieldValue,6) = 0 then 4 Else DefaultAttribute

The source data is a field that is formated to 5 decimal places regardless of the actual number of decimals (ex 1.23 = 1.23000). The problem is that my formula doesn't work for some reason. I get results that are not in line with what I would expect. Some fields display 2 decimals sometimes and others display 5.

Can anyone help me?

Thanks!
 
Crystalyzer,

I had this linked bookmarked.

"Convert Decimal Numbers to Text showing only the non-zero decimals:"
__________________________________________________________
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 ]
 
Thanks Campsys!

You got me to thinking in a different way and then I was able to modify the solution so that my result was STILL a number and not text. Here the code that I used in the "Format Field\Number\Customize\Decimals\x-2" section:
Code:
WhileReadingRecords ; 
StringVar text :=  Totext ( {Activity.Quantity} , 5) ;
NumberVar end  :=  length ( text ) ; 
NumberVar clip  := 
IIF ( Val ( text [ end - 0 to end ] ) <> 0, 5,
IIF ( Val ( text [ end - 1 to end ] ) <> 0, 4,
IIF ( Val ( text [ end - 2 to end ] ) <> 0, 3,
IIF ( Val ( text [ end - 3 to end ] ) <> 0, 2,
IIF ( Val ( text [ end - 4 to end ] ) <> 0, 1, 0))))) ;

clip
The resulting numbers from the IIF statements are stored in the NumVar "clip" and indicate the number of decimal places to show without having to convert the result to text. This way I can still subtotal on the field.

Thanks and best regards!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top