Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

TxqSciNumberFormat.h

Go to the documentation of this file.
00001 //---------------------------------------------------------------------------
00002 //
00003 // File:    TxqSciNumberFormat.h
00004 //
00005 // Purpose:  a class for producing a string from a double
00006 //
00007 // Version: $Id: TxqSciNumberFormat.h,v 1.6 2001/10/12 18:51:23 kgl Exp $
00008 //
00009 // This class name comes from Ronan's Jplot package.
00010 //
00011 //---------------------------------------------------------------------------
00012 
00013 #ifndef TXQ_SCINUMBERFORMAT_H
00014 #define TXQ_SCINUMBERFORMAT_H
00015 
00016 // system includes
00017 // #include <cstdio>
00018 #include <vector>
00019 
00020 // Txq includes
00021 #include "TxqSciNumberPattern.h"
00022 #include "TxqFloatingDecimal.h"
00023 
00024 // Qt includes
00025 #include <qstring.h>
00026 
00051 class TxqSciNumberFormat
00052 {
00053 
00054 public:
00055 
00060     TxqSciNumberFormat(TxqSciNumberPattern p)
00061     {
00062        pattern = p;
00063     }
00064 
00068    ~TxqSciNumberFormat(){}
00069 
00075     QString format(double d) {
00076 
00077     //
00078     // local copies of FloatingDecimal values
00079     // and needed variables
00080     //
00081        int nDigits           = pattern.numPreDecimal +
00082                                 pattern.numPostDecimal;
00083                                 // pattern.numExp;
00084                                 
00085        TxqFloatingDecimal fd(d, nDigits);
00086 
00087        bool isNegative       = fd.isNegative();
00088        bool isExceptional    = fd.isExceptional();
00089        int decExponent       = fd.getExponent();
00090            std::vector<QString> digits;
00091     //
00092     // local copies of pattern variables
00093     //
00094        int numPreDecDigits  = pattern.numPreDecimal;
00095        int numPostDecDigits = pattern.numPostDecimal;
00096        int numExpDigits     = pattern.numExp;
00097 
00098        int expAdjust = numPreDecDigits-1;
00099 
00100     // 
00101     // get the number of digits into the digit array
00102     //
00103            int i;
00104        for (i=0; i<nDigits; i++)  {
00105            QString tmpDigit;
00106            digits.push_back(tmpDigit.sprintf("%d",fd.getDigit()));
00107        }
00108           
00109     // formatted value will go in result char[]
00110     // make pletty of room for number of digits plus special characters
00111        QString result;
00112 
00113     // i keeps count of where we are in result array
00114     // negative value, push a minus sign on
00115        if (isNegative)
00116        {
00117            result = '-';
00118            i = 1;
00119        }
00120 
00121      // NaN or infinite - treat as speical case
00122        if (isExceptional)
00123        {
00124           // cerr << "TxqSciNumberFormat::Number is too big or too small " << endl;
00125        }
00126 
00127        else
00128        {
00129            int exp = decExponent;
00130            int c;
00131            for (c = 0; c < numPreDecDigits; c++)
00132            {
00133               if (c < nDigits) result += digits[c] ;
00134               else result += '0';
00135               // result += (c < nDigits) ? digits[c] : '0';
00136            }
00137 
00138            // adjust exponential
00139            // exp -= numPreDecDigits;
00140            int sign = fd.getExponentSign();
00141            if (expAdjust != 0) {
00142               exp -= expAdjust;
00143               int esign = 0;
00144               if ( (exp < 1.0) && (exp!= 0) ) esign = -1;
00145               if (sign != esign) sign = esign;
00146            }
00147 
00148            // insert decimal point
00149            result += '.';
00150            i++;
00151 
00152            // copy format.post bytes from digits[format.pre] to result[i]
00153            for (c = 0; c < numPostDecDigits; c++)
00154            {
00155               int srcIndex = numPreDecDigits + c;
00156               i++;
00157               if (srcIndex < nDigits) result += digits[srcIndex];
00158               else  result += '0';
00159               // result += (srcIndex < nDigits) ? digits[srcIndex] : '0';
00160               // result += (srcIndex < nDigits) ? digits[srcIndex] : '0';
00161            }
00162 
00163            // insert exponential sign
00164            result += 'E';
00165            i++;
00166 
00167            // insert +/- sign for exponential
00168            // result += (fd.getExponentSign() < 0) ? '-' : '+';
00169            // result += (fd.getExponentSign() == -1 ) ? '-' : '+';
00170            result += (sign == -1 ) ? '-' : '+';
00171            i++;
00172 
00173            // force exponent to be positive
00174            exp = abs(exp);
00175 
00176            // exponent has 1, 2, or 3, digits
00177            if (exp <= 9)
00178            {
00179               if (numExpDigits >= 3) {
00180                   result += '0';
00181                   i++;
00182               }
00183               if (numExpDigits >= 2) {
00184                   result += '0';
00185                   i++;
00186               }
00187               result += (char)(exp + '0');
00188               i++;
00189            }
00190            else if (exp <= 99)
00191            {
00192               if (numExpDigits >= 3) {
00193                   result += '0';
00194                   i++;
00195               }
00196               result  += (char)(exp/10 + '0' );
00197               i++;
00198               result  += (char)(exp%10 + '0' );
00199               i++;
00200            }
00201            else
00202            {
00203               result += (char)(exp/100 + '0');
00204               i++;
00205               exp %= 100;
00206               result += (char)(exp/10 + '0');
00207               i++;
00208               result += (char)(exp%10 + '0' );
00209               i++;
00210            }
00211        }
00212 
00213        return result;
00214 
00215     }
00216 
00217 private:
00218 
00222     TxqSciNumberPattern pattern;
00223 };
00224 #endif
Copyright Tech-X Corporation, all rights reserved.