Index  Source Files  Annotated Class List  Alphabetical Class List  Class Hierarchy  Graphical Class Hierarchy   
 

FIX::DoubleConvertor Struct Reference

Converts double to/from a string. More...

#include <FieldConvertors.h>

List of all members.

Static Public Member Functions

std::string convert (double value, int padding=0)
bool convert (const std::string &value, double &result)
double convert (const std::string &value) throw ( FieldConvertError )


Detailed Description

Converts double to/from a string.

Definition at line 201 of file FieldConvertors.h.


Member Function Documentation

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         }

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         }

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 convert().

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         }


The documentation for this struct was generated from the following file:
Generated on Mon Jul 24 19:36:42 2006 for QuickFIX by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2001