ACF Library

DisplayEngSciFormat

Back to List

Description: Convert numeric values into engineering format or scientific format

FileMaker Prototype:

Set Variable [$res; ACF_Run("DisplayEngSciFormat"; string_val;  string_ftype;  string_unit)]

Category: MATH

Function source:

function DisplayEngSciFormat ( string val, string ftype, string unit ) 
   val = substitute ( val, ",", "."); // turn commas into dot. 
   double value = double (trimboth(val));
   string mantissa, sExp, result;
   int exp,num, exp2; 
   if ( value == 0) then
      return "0"; 
   end if
   case
      :(left(ftype,1) == "E") // Engineering format
         exp2 = int((log10(value))/3)*3;
         value = value / 10^exp2;
          // Ensure mantissa is in the range [1, 1000)
          if ( value < 1 ) then
              exp2 = exp2 - 3;
              value = value * 1000;
          end if
   
         // Format mantissa without trailing zeros
         mantissa = format("%.10f", value);  // Use high precision initially
         // Trim trailing zeros
         while ( right(mantissa, 1) == "0" ) 
             mantissa = left(mantissa, length(mantissa) - 1);
         end while
         // Remove any remaining trailing period
         if ( right(mantissa, 1) == "." ) then
             mantissa = left(mantissa, length(mantissa) - 1);
         end if
         if ( ftype == "EK" && abs(exp2)<13) then // Using K, M, p, etc instead. 
               exp = int(abs(exp2/3)); // 12/3=4 = p; 9/3=3=n, 6/3=2 = u, 3/3=1=m, 0/3=0 = " "
            
               if (exp2 <= 0) then
                  sExp = substring ( " mµnp",exp,1); 
               else
                  sExp = substring ( " KMGT",exp,1); // 3/3=K, 6/3=M, 9/3=G, 12/3T
               end if
               result = mantissa + " "+sExp; 
         else
               result =  mantissa + "E" + ((Exp2>=0)?"+":"")+string(exp2);
         end if
       :(ftype == "S")
          result = format("%E",Value);
       default
          result = val; 
   end case
   
   return result+unit; 
end 

The DisplayEngSciFormat function is a versatile tool for converting very small or large numbers into either engineering or scientific notation.

  • Engineering format (E): Displays a mantissa and an exponent, where the exponent is a multiple of 3. Possible exponents include -12, -9, -6, -3, 0, 3, 6, 9, 12, etc.
  • Scientific format (S): Displays a mantissa with 1 digit before the decimal point and an exponent that is not restricted to multiples of 3.
  • Engineering format with SI sufixes (EK): Uses SI sufixes (p, n, μ, m, " ", K, M, G, T) for exponents within the range of -12 to 12, following the engineering notation rules for the exponent.
  • Unit addition: Optionally appends a unit to the formatted result.

Examples

Set Variable [$res; ACF_Run("DisplayEngSciFormat"; 2000; "EK"; "Ohm")]
=> "2 KOhm"
Set Variable [$res; ACF_Run("DisplayEngSciFormat"; 1.5E4; "EK"; "Ohm")]
=> "15 KOhm"
Set Variable [$res; ACF_Run("DisplayEngSciFormat"; 47E-10; "EK"; "F")]
=> "4.7 nF"
Set Variable [$res; ACF_Run("DisplayEngSciFormat"; 20050000; "E"; "")]
=> "20.05E+6"
Set Variable [$res; ACF_Run("DisplayEngSciFormat"; 20050000; "S"; "")]
=> "2.005000E+07"
Back to List