#########################################################################
| 105 | |
| 106 | // ######################################################################### |
| 107 | QString misc::StringNiceNum(double num) |
| 108 | { |
| 109 | char Format[6] = "%.8e"; |
| 110 | if(fabs(num) < 1e-250) return QString("0"); // avoid many problems |
| 111 | if(fabs(log10(fabs(num))) < 3.0) Format[3] = 'g'; |
| 112 | |
| 113 | int a = 0; |
| 114 | char *p, *pe, Buffer[512]; |
| 115 | |
| 116 | sprintf(Buffer, Format, num); |
| 117 | p = pe = strchr(Buffer, 'e'); |
| 118 | if(p) { |
| 119 | if(*(++p) == '+') { a = 1; } // remove '+' of exponent |
| 120 | if(*(++p) == '0') { a++; p++; } // remove leading zeros of exponent |
| 121 | if(a > 0) |
| 122 | do { |
| 123 | *(p-a) = *p; |
| 124 | } while(*(p++) != 0); // override characters not needed |
| 125 | |
| 126 | // In 'g' format, trailing zeros are already cut off !!! |
| 127 | p = strchr(Buffer, '.'); |
| 128 | if(p) { |
| 129 | if(!pe) pe = Buffer + strlen(Buffer); |
| 130 | p = pe-1; |
| 131 | while(*p == '0') // looking for unneccessary zero characters |
| 132 | if((--p) <= Buffer) break; |
| 133 | if(*p != '.') p++; // no digit after decimal point ? |
| 134 | while( (*(p++) = *(pe++)) != 0 ) ; // overwrite zero characters |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return QString(Buffer); |
| 139 | } |
| 140 | |
| 141 | // ######################################################################### |
| 142 | void misc::str2num(const QString& s_, double& Number, QString& Unit, double& Factor) |