Format Precision & Scale

This is a discussion on Format Precision & Scale within the C Programming forums, part of the General Programming Boards category; Hi, I've just been asked to make a change to a legacy C app. and I need help in trying ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Format Precision & Scale

    Hi,

    I've just been asked to make a change to a legacy C app. and I need help in trying to figure out what the existing code is doing.

    Essentially, what the app does is it runs some SQL against a database and writes the resultset out to a .txt file. As it writes the data to the file it formats each cell individually depending on the datatype.

    Currently, the app. will format a Double value using 6 decimal places only e.g. 376.123456. I've been asked to change it to 8 decimal places e.g. 376.12345678.

    I've had a look at the code and I think I've isolated the section that I need to change (in bold below) but I'm having difficulty figuring out extacly HOW to change it!!!

    Any help would be very much appreciated.....

    Code:
    /*---------------------------------------------------------------------------
    	Proc :	DumpCellData
    
    	Desc :	Write a unit of data to the output file
    
    	Param:	sqldata			- the pointer of the data
    			sqltype			- the type of the data
    			sqlind			- 
    			sqllen			- the length of the data
    			colWidth		- the width of the data to be outputed
    			outputFile		- the output file pointer
    		
    	Return:	0 - if successful
     *--------------------------------------------------------------------------*/
    
    int DumpCellData(char *sqldata, short sqltype, short *sqlind, short sqllen, short colWidth, FILE *outputFile)
    {
      int rc = 0;
      char buf[100];
      struct lstr
      {
        char len;
        char *data;
      } *pLstr;
      int i;
      short precision;
      short scale;
      short numBytes;
      short top;
      short bottom;
      short byteNb;
      short digNb;
      struct lob
      {
        sqlint32 len;
        char *data;
      } *pLob;
    
      if (sqltype & 1 && *sqlind < 0)
      {
        /* printf("%-*s", colWidth, " -"); */
      }
      else
      {
        switch (sqltype)
        {
          case SQL_TYP_DATE:
          case SQL_TYP_NDATE:
          case SQL_TYP_TIME:
          case SQL_TYP_NTIME:
          case SQL_TYP_STAMP:
          case SQL_TYP_NSTAMP:
          case SQL_TYP_VARCHAR:
          case SQL_TYP_NVARCHAR:
          case SQL_TYP_CHAR:
          case SQL_TYP_NCHAR:
          case SQL_TYP_LONG:
          case SQL_TYP_NLONG:
          case SQL_TYP_CSTR:
          case SQL_TYP_NCSTR:
            fprintf(outputFile, "%-*.*s", colWidth, colWidth, sqldata);
            break;
          case SQL_TYP_LSTR:
          case SQL_TYP_NLSTR:
            pLstr = (struct lstr *)sqldata;
            for (i = 0; i < (int)pLstr->len; i++)
            {
              fprintf(outputFile, "%c", (pLstr->data)[i]);
            }
            while (i < colWidth)
            {
              fprintf(outputFile, " ");
              i = i + 1;
            }
            break;
          case SQL_TYP_FLOAT:
          case SQL_TYP_NFLOAT:
          	/* Jacky Lam 17/04/2003 */
            fprintf(outputFile, "%*f", colWidth, *((double *)sqldata));
            break;
          case SQL_TYP_INTEGER:
          case SQL_TYP_NINTEGER:
            fprintf(outputFile, "%*d", colWidth, *((int *)sqldata));
            /* Jacky Lam 17/04/2003
            fprintf(outputFile, "%*ld", colWidth, *((long *)sqldata)); */
            break;
          case SQL_TYP_SMALL:
          case SQL_TYP_NSMALL:
            fprintf(outputFile, "%*d", colWidth, *((short *)sqldata));
            break;
          case SQL_TYP_DECIMAL:
          case SQL_TYP_NDECIMAL:
            precision = (short)(((char *)&sqllen)[0]);
            scale = (short)(((char *)&sqllen)[1]);
    
            /* adjust the precision to odd value */
            if ((precision % 2) == 0)
            {
              precision = precision + 1;
            }
    
            /* calculate numBytes */
            numBytes = (short)(precision + 1) / 2;
    
            /* determine the sign using bottom of the last byte */
            bottom = *(sqldata + numBytes - 1) & 0x000F;
            i = 0;
            if (bottom == 0x000D || bottom == 0x000B)
            {
              buf[i] = '-';
              i++;
            }
            else
            {
              buf[i] = ' ';
              i++;
            }
    
            /* prepare the decimal number */
            digNb = 0;
            if (digNb == precision - scale)
            {
              buf[i] = '.';
              i++;
            }
    
            /* (top + bottom) from first (numBytes - 1) bytes ... */
            for (byteNb = 0; byteNb < numBytes - 1; byteNb = byteNb + 1)
            {
              top = *(sqldata + byteNb) & 0x00F0;
              top = top >> 4;
              bottom = *(sqldata + byteNb) & 0x000F;
              buf[i] = top + '0';
              i++;
              digNb++;
              if (digNb == precision - scale)
              {
                buf[i] = '.';
                i++;
              }
              buf[i] = bottom + '0';
              i++;
              digNb++;
              if (digNb == precision - scale)
              {
                buf[i] = '.';
                i++;
              }
            }
    
            /* ... and top of the last byte (bottom is the sign) */
            top = *(sqldata + byteNb) & 0x00F0;
            top = top >> 4;
            buf[i] = top + '0';
            i++;
            digNb++;
            if (digNb == precision - scale)
            {
              buf[i] = '.';
              i++;
            }
    
            /* display decimal number */
            buf[i] = '\0';
            fprintf(outputFile, "%*s", colWidth, buf);
            break;      case SQL_TYP_CGSTR:
          case SQL_TYP_NCGSTR:
          case SQL_TYP_VARGRAPH:
          case SQL_TYP_NVARGRAPH:
          case SQL_TYP_GRAPHIC:
          case SQL_TYP_NGRAPHIC:
          case SQL_TYP_LONGRAPH:
          case SQL_TYP_NLONGRAPH:
            sprintf(buf, "Graphic data length: %5ld", sqllen);
            fprintf(outputFile, "%-*s", colWidth, buf);
            break;
          case SQL_TYP_BLOB:
          case SQL_TYP_NBLOB:
          case SQL_TYP_CLOB:
          case SQL_TYP_NCLOB:
          case SQL_TYP_DBCLOB:
          case SQL_TYP_NDBCLOB:
            pLob = (struct lob *)sqldata;
            if (*sqlind == 0)
            {
              sprintf(buf, "LOB length: %10ld", (long)pLob->len);
            }
            else
            {
              sprintf(buf, "Truncated LOB length: %10ld", (long)pLob->len);
            }
            fprintf(outputFile, "%-*s", colWidth, buf);
            break;
          default:
            fprintf(outputFile, "%-*s", colWidth, "?");
            break;
        }
      }
    
      return 0;
    } /* DumpCellData */

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    ...any clues...or pointers...thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. Scale in perfmon tool
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 02-03-2008, 12:54 AM
  3. format specifier (precision) in strings
    By zombiezparadize in forum C Programming
    Replies: 2
    Last Post: 06-12-2007, 02:13 PM
  4. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  5. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21