Static Public Member Functions | 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 Private Member Functions

static double fast_atof (const char *p)
 

Detailed Description

Converts double to/from a string.

Definition at line 304 of file FieldConvertors.h.

Member Function Documentation

◆ convert() [1/3]

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

Definition at line 401 of file FieldConvertors.h.

References STRING_SPRINTF.

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

402  {
403  char result[32];
404  char *end = 0;
405 
406  int size;
407  if( value == 0 || value > 0.0001 || value <= -0.0001 )
408  {
409  size = STRING_SPRINTF( result, "%.15g", value );
410 
411  if( padding > 0 )
412  {
413  char* point = result;
414  end = result + size - 1;
415  while( *point != '.' && *point != 0 )
416  point++;
417 
418  if( *point == 0 )
419  {
420  end = point;
421  *point = '.';
422  size++;
423  }
424  int needed = padding - (int)(end - point);
425 
426  while( needed-- > 0 )
427  {
428  *(++end) = '0';
429  size++;
430  }
431  *(end+1) = 0;
432  }
433  }
434  else
435  {
436  size = STRING_SPRINTF( result, "%.15f", value );
437  // strip trailing 0's
438  end = result + size - 1;
439 
440  if( padding > 0 )
441  {
442  int discard = 15 - padding;
443 
444  while( (*end == '0') && (discard-- > 0) )
445  {
446  *(end--) = 0;
447  size--;
448  }
449  }
450  else
451  {
452  while( *end == '0' )
453  {
454  *(end--) = 0;
455  size--;
456  }
457  }
458  }
459 
460  return std::string( result, size );
461 }
#define STRING_SPRINTF
Definition: Utility.h:196

◆ convert() [2/3]

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

Definition at line 463 of file FieldConvertors.h.

References IS_DIGIT.

464 {
465  const char * i = value.c_str();
466 
467  // Catch null strings
468  if( !*i ) return false;
469  // Eat leading '-' and recheck for null string
470  if( *i == '-' && !*++i ) return false;
471 
472  bool haveDigit = false;
473 
474  if( IS_DIGIT(*i) )
475  {
476  haveDigit = true;
477  while( IS_DIGIT (*++i) );
478  }
479 
480  if( *i == '.' && IS_DIGIT(*++i) )
481  {
482  haveDigit = true;
483  while( IS_DIGIT (*++i) );
484  }
485 
486  if( *i || !haveDigit ) return false;
487 
488  result = fast_atof( value.c_str() );
489  return true;
490  }
#define IS_DIGIT(x)
static double fast_atof(const char *p)

◆ convert() [3/3]

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

Definition at line 492 of file FieldConvertors.h.

References FIX::EmptyConvertor::convert().

494  {
495  double result = 0.0;
496  if( !convert( value, result ) )
497  throw FieldConvertError(value);
498  else
499  return result;
500  }
static std::string convert(double value, int padding=0)

◆ fast_atof()

static double FIX::DoubleConvertor::fast_atof ( const char *  p)
inlinestaticprivate

Definition at line 316 of file FieldConvertors.h.

References IS_DIGIT, and IS_SPACE.

317  {
318  bool frac(false);
319  double sign(1.), value(0.), scale(1.);
320 
321  while (IS_SPACE(*p))
322  ++p;
323 
324  // Get sign, if any.
325  if (*p == '-')
326  {
327  sign = -1.;
328  ++p;
329  }
330  else if (*p == '+')
331  ++p;
332 
333  // Get digits before decimal point or exponent, if any.
334  while (IS_DIGIT(*p))
335  {
336  value = value * 10. + (*p - '0');
337  ++p;
338  }
339 
340  // Get digits after decimal point, if any.
341  if (*p == '.')
342  {
343  ++p;
344  double pow10(10.);
345  while (IS_DIGIT(*p))
346  {
347  value += (*p - '0') / pow10;
348  pow10 *= 10.;
349  ++p;
350  }
351  }
352 
353  // Handle exponent, if any.
354  if (toupper(*p) == 'E')
355  {
356  unsigned int expon(0);
357  ++p;
358 
359  // Get sign of exponent, if any.
360  if (*p == '-')
361  {
362  frac = true;
363  ++p;
364  }
365  else if (*p == '+')
366  ++p;
367 
368  // Get digits of exponent, if any.
369  while (IS_DIGIT(*p))
370  {
371  expon = expon * 10 + (*p - '0');
372  ++p;
373  }
374  if (expon > 308)
375  expon = 308;
376 
377  // Calculate scaling factor.
378  while (expon >= 50)
379  {
380  scale *= 1E50;
381  expon -= 50;
382  }
383  while (expon >= 8)
384  {
385  scale *= 1E8;
386  expon -= 8;
387  }
388  while (expon > 0)
389  {
390  scale *= 10.0;
391  expon -= 1;
392  }
393  }
394 
395  // Return signed and scaled floating point result.
396  return sign * (frac ? (value / scale) : (value * scale));
397  }
#define IS_DIGIT(x)
#define IS_SPACE(x)

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

Generated on Thu Sep 5 2019 11:07:58 for QuickFIX by doxygen 1.8.13 written by Dimitri van Heesch, © 1997-2001