![]() |
Converts double to/from a string. More...
#include <FieldConvertors.h>
Static Public Member Functions | |
static std::string | convert (double value, int padding=0) |
static bool | convert (const std::string &value, double &result) |
static double | convert (const std::string &value) throw ( FieldConvertError ) |
Converts double to/from a string.
Definition at line 201 of file FieldConvertors.h.
static double FIX::DoubleConvertor::convert | ( | const std::string & | value | ) | throw ( FieldConvertError ) [inline, static] |
Definition at line 293 of file FieldConvertors.h.
References convert().
00295 { 00296 double result = 0.0; 00297 if( !convert( value, result ) ) 00298 throw FieldConvertError(); 00299 else 00300 return result; 00301 }
static bool FIX::DoubleConvertor::convert | ( | const std::string & | value, | |
double & | result | |||
) | [inline, static] |
Definition at line 265 of file FieldConvertors.h.
00266 { 00267 const char * i = value.c_str(); 00268 00269 // Catch null strings 00270 if( !*i ) return false; 00271 // Eat leading '-' and recheck for null string 00272 if( *i == '-' && !*++i ) return false; 00273 00274 bool haveDigit = false; 00275 00276 if( isdigit(*i) ) 00277 { 00278 haveDigit = true; 00279 while( isdigit (*++i) ); 00280 } 00281 00282 if( *i == '.' && isdigit(*++i) ) 00283 { 00284 haveDigit = true; 00285 while( isdigit (*++i) ); 00286 } 00287 00288 if( *i || !haveDigit ) return false; 00289 result = strtod( value.c_str(), 0 ); 00290 return true; 00291 }
static std::string FIX::DoubleConvertor::convert | ( | double | value, | |
int | padding = 0 | |||
) | [inline, static] |
Definition at line 203 of file FieldConvertors.h.
References STRING_SPRINTF.
Referenced by FIX::DataDictionary::checkValidFormat(), convert(), FIX::Dictionary::getDouble(), and FIX::Dictionary::setDouble().
00204 { 00205 char result[32]; 00206 char *end = 0; 00207 00208 int size; 00209 if( value == 0 || value > 0.0001 || value <= -0.0001 ) 00210 { 00211 size = STRING_SPRINTF( result, "%.15g", value ); 00212 00213 if( padding > 0 ) 00214 { 00215 char* point = result; 00216 end = result + size - 1; 00217 while( *point != '.' && *point != 0 ) 00218 point++; 00219 00220 if( *point == 0 ) 00221 { 00222 end = point; 00223 *point = '.'; 00224 size++; 00225 } 00226 int needed = padding - (int)(end - point); 00227 00228 while( needed-- > 0 ) 00229 { 00230 *(++end) = '0'; 00231 size++; 00232 } 00233 *(end+1) = 0; 00234 } 00235 } 00236 else 00237 { 00238 size = STRING_SPRINTF( result, "%.15f", value ); 00239 // strip trailing 0's 00240 end = result + size - 1; 00241 00242 if( padding > 0 ) 00243 { 00244 int discard = 15 - padding; 00245 00246 while( (*end == '0') && (discard-- > 0) ) 00247 { 00248 *(end--) = 0; 00249 size--; 00250 } 00251 } 00252 else 00253 { 00254 while( *end == '0' ) 00255 { 00256 *(end--) = 0; 00257 size--; 00258 } 00259 } 00260 } 00261 00262 return std::string( result, size ); 00263 }