SAP Knowledge Base Article - Public

2674314 - How to calculate the result of an equation stored in a text field in Crystal Reports?

Symptom

  • How to perform calculation on a string?
  • How to calculate the result of an equation stored in a text field in Crystal Reports?
  • When a string field contains values, like: 45 * 100, how do we calculate the result in Crystal Reports?

Environment

  • SAP Crystal Reports 2008
  • SAP Crystal Reports 2011
  • SAP Crystal Reports 2013
  • SAP Crystal Reports 2016

Cause

  • There is no functionality in Crystal Reports that will calculate the result of an equation stored in a string field.

Resolution

  • Depending on the complexity of the equation stored in the string field, it might be possible to create a formula in Crystal Reports that convert the numeric values stored in the string, to actual numeric values, and then calculate the result in the formula.
       
  • For example:

    If a database stores equation in a string field like: 
         
        10 * 5
        50 * 2.5
        1 * 2 * 3 * 4   
       
    Then it is possible to create a formula in Crystal Reports, that convert the text to numerical values, using the function "ToNumber", and then multiply the numbers, and return the results.
          
    The formula will look like:  

         WhileReadingRecords;
         Local StringVar stringEquation := {INSERT STRING FIELD EQUATION HERE}; // Insert your string equation here.

         Local NumberVar Result := 1;

         If "." in stringEquation Then
              stringEquation := Replace(stringEquation,".",",");

         If "*" in stringEquation Then
         (
              Local StringVar Array aEquation := Split(stringEquation,"*");
              Local NumberVar i;
              For i := 1 to UBound(aEquation) Do
              (
      
              If isNumeric(aEquation[i]) Then
                   Result := Result * ToNumber(aEquation[i]);
         );
         Result;
         ) 
         Else If isNumeric(stringEquation) Then
              Result := ToNumber(stringEquation)
         Else
              Result := 0;

         Result
        
          

  • Sample report: Calculating Result of a String Equation.rpt 
           
     Note: The sample report, can be downloaded by selecting it from the Attachments section below. 

Keywords

CR, String Equation , KBA , BI-RA-CR , Crystal Reports designer or BusinessViews Manager , How To

Product

Crystal Reports 2008 V1 ; SAP Crystal Reports 2011 ; SAP Crystal Reports 2013 ; SAP Crystal Reports 2016

Attachments

Calculating Result of a String Equation.rpt