How do I adjust the number of decimals for Quantity or Price fields in Crystal Reports?

Steps on how to change the number of decimal places dynamically for Quantity and Price database fields in Crystal Reports.

Crystal Reports does not include an option to automatically adjust the number of decimals for a Number field. However, this can be accomplished with a simple formula for each field on the report.

  1. Right-click on the Field and choose Format Field…
  2. Select the Number tab
  3. Click Customize button
  4. Select Number tab
  5. Use the drop-down next to Decimals to select the maximum number of decimals (recommended = 1.0000000)
  6. Click X+2 button to the right of Decimals and paste formula code listed below
  7. Adjust the minDecimals to be the minimum number of decimals to be displayed (currently 0)
  8. Use the drop-down next to Rounding to select the maximum number of decimals to be rounded (recommended = 0.0000001)
  9. Click X+2 button to the right of Rounding and paste formula code listed below
  10. Adjust the minDecimals to be the minimum number of decimals to be displayed (currently 0)

Formula Code:

WhilePrintingRecords;
numberVar counter := 0;
numberVar minDecimals := 0;
numberVar num := CurrentFieldValue;
while truncate(num) <> num and counter < 10 do
(num := num * 10;
counter := counter + 1);
if counter < minDecimals then
minDecimals
else
counter;