Static Public Member Functions | Static Public Attributes | Static Private Member Functions | List of all members
FIX::DoubleConvertor Struct Reference

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 )
 

Static Public Attributes

static const int SIGNIFICANT_DIGITS = 15
 
static const int BUFFFER_SIZE = 32
 

Static Private Member Functions

static double fast_strtod (const char *buffer, int size, int *processed_chars)
 
static int fast_dtoa (char *buffer, int size, double value)
 
static int fast_fixed_dtoa (char *buffer, int size, double value)
 

Detailed Description

Converts double to/from a string.

Definition at line 239 of file FieldConvertors.h.

Member Function Documentation

◆ convert() [1/3]

static double FIX::DoubleConvertor::convert ( const std::string &  value)
throw (FieldConvertError
)
inlinestatic

Definition at line 360 of file FieldConvertors.h.

362 {
363 double result = 0.0;
364 if( !convert( value, result ) )
365 throw FieldConvertError(value);
366 else
367 return result;
368 }
static std::string convert(double value, int padding=0)

References convert().

◆ convert() [2/3]

static bool FIX::DoubleConvertor::convert ( const std::string &  value,
double &  result 
)
inlinestatic

Definition at line 321 of file FieldConvertors.h.

322{
323 const char * i = value.c_str();
324
325 // Catch null strings
326 if( !*i ) return false;
327 // Eat leading '-' and recheck for null string
328 if( *i == '-' && !*++i ) return false;
329
330 bool haveDigit = false;
331
332 if( IS_DIGIT(*i) )
333 {
334 haveDigit = true;
335 while( IS_DIGIT (*++i) );
336 }
337
338 if( *i == '.' && IS_DIGIT(*++i) )
339 {
340 haveDigit = true;
341 while( IS_DIGIT (*++i) );
342 }
343
344 if( *i || !haveDigit ) return false;
345
346 int processed_chars;
347 const int total_length = value.length();
348 const double val = fast_strtod( value.c_str(), total_length, &processed_chars);
349
350 if ( processed_chars != total_length ||
351 val != val /*test for quite NaN*/ )
352 {
353 return false;
354 }
355
356 result = val;
357 return true;
358}
#define IS_DIGIT(x)
static double fast_strtod(const char *buffer, int size, int *processed_chars)

References fast_strtod(), and IS_DIGIT.

◆ convert() [3/3]

static std::string FIX::DoubleConvertor::convert ( double  value,
int  padding = 0 
)
inlinestatic

Definition at line 255 of file FieldConvertors.h.

256 {
257 char result[BUFFFER_SIZE];
258 char *end = 0;
259
260 int size;
261 if( value == 0 || value > 0.0001 || value < -0.0001 )
262 {
263 size = fast_dtoa( result, BUFFFER_SIZE, value );
264 if( size == 0 )
265 return std::string();
266
267 if( padding > 0 )
268 {
269 char* point = result;
270 end = result + size - 1;
271 while( *point != '.' && *point != 0 )
272 point++;
273
274 if( *point == 0 )
275 {
276 end = point;
277 *point = '.';
278 ++size;
279 }
280 int needed = padding - (int)(end - point);
281
282 if( needed > 0 )
283 {
284 memset( ++end, '0', needed );
285 size += needed;
286 }
287 }
288 }
289 else
290 {
291 size = fast_fixed_dtoa( result, BUFFFER_SIZE, value );
292 if( size == 0 )
293 return std::string();
294
295 // strip trailing 0's
296 end = result + size - 1;
297
298 if( padding > 0 )
299 {
300 int discard = SIGNIFICANT_DIGITS - padding;
301
302 while( (*end == '0') && (discard-- > 0) )
303 {
304 --end;
305 --size;
306 }
307 }
308 else
309 {
310 while( *end == '0' )
311 {
312 --end;
313 --size;
314 }
315 }
316 }
317
318 return std::string( result, size );
319}
static int fast_fixed_dtoa(char *buffer, int size, double value)
static const int BUFFFER_SIZE
static int fast_dtoa(char *buffer, int size, double value)
static const int SIGNIFICANT_DIGITS

References BUFFFER_SIZE, fast_dtoa(), fast_fixed_dtoa(), and SIGNIFICANT_DIGITS.

Referenced by FIX::DataDictionary::checkValidFormat(), convert(), FIX::Dictionary::getDouble(), FIX::DoubleField::getValue(), FIX::Dictionary::setDouble(), and FIX::DoubleField::setValue().

◆ fast_dtoa()

int FIX::DoubleConvertor::fast_dtoa ( char *  buffer,
int  size,
double  value 
)
staticprivate

Definition at line 68 of file FieldConvertors.cpp.

69 {
70 double_conversion::StringBuilder builder( buffer, size );
71 if( !g_dtoa_converter.ToPrecision( value, DoubleConvertor::SIGNIFICANT_DIGITS, &builder ) )
72 {
73 builder.Reset();
74 return 0;
75 }
76
77 builder.TrimTrailingZeros();
78 return builder.position();
79 }
static double_conversion::DoubleToStringConverter g_dtoa_converter(double_conversion::DoubleToStringConverter::NO_FLAGS, "INF", "NAN", 'e', -DoubleConvertor::SIGNIFICANT_DIGITS, DoubleConvertor::SIGNIFICANT_DIGITS, DoubleConvertor::SIGNIFICANT_DIGITS - 1, 0)

References FIX::g_dtoa_converter(), and SIGNIFICANT_DIGITS.

Referenced by convert().

◆ fast_fixed_dtoa()

int FIX::DoubleConvertor::fast_fixed_dtoa ( char *  buffer,
int  size,
double  value 
)
staticprivate

Definition at line 81 of file FieldConvertors.cpp.

82 {
83 double_conversion::StringBuilder builder( buffer, size );
84 if( !g_dtoa_converter.ToFixed( value, DoubleConvertor::SIGNIFICANT_DIGITS, &builder ) )
85 {
86 builder.Reset();
87 return 0;
88 }
89
90 return builder.position();
91 }

References FIX::g_dtoa_converter(), and SIGNIFICANT_DIGITS.

Referenced by convert().

◆ fast_strtod()

double FIX::DoubleConvertor::fast_strtod ( const char *  buffer,
int  size,
int *  processed_chars 
)
staticprivate

Definition at line 63 of file FieldConvertors.cpp.

64 {
65 return g_atod_converter.StringToDouble( buffer, size, processed_chars );
66 }
static double_conversion::StringToDoubleConverter g_atod_converter(double_conversion::StringToDoubleConverter::NO_FLAGS, std::numeric_limits< double >::quiet_NaN(), std::numeric_limits< double >::quiet_NaN(), "INF", "NAN")

References FIX::g_atod_converter().

Referenced by convert().

Member Data Documentation

◆ BUFFFER_SIZE

const int FIX::DoubleConvertor::BUFFFER_SIZE = 32
static

Definition at line 253 of file FieldConvertors.h.

Referenced by convert().

◆ SIGNIFICANT_DIGITS

const int FIX::DoubleConvertor::SIGNIFICANT_DIGITS = 15
static

Definition at line 252 of file FieldConvertors.h.

Referenced by convert(), fast_dtoa(), and fast_fixed_dtoa().


The documentation for this struct was generated from the following files:

Generated on Thu May 22 2025 08:23:50 for QuickFIX by doxygen 1.9.8 written by Dimitri van Heesch, © 1997-2001