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`
| ) |  |

inline static

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

{
char result[32];
char *end = 0;
int size;
if( value == 0 || value > 0.0001 || value <= -0.0001 )
{
size = STRING_SPRINTF( result, "%.15g", value );
if( padding > 0 )
{
char* point = result;
end = result + size - 1;
while( *point != '.' && *point != 0 )
point++;
if( *point == 0 )
{
end = point;
*point = '.';
size++;
}
int needed = padding - (int)(end - point);
while( needed-- > 0 )
{
*(++end) = '0';
size++;
}
*(end+1) = 0;
}
}
else
{
size = STRING_SPRINTF( result, "%.15f", value );
// strip trailing 0's
end = result + size - 1;
if( padding > 0 )
{
int discard = 15 - padding;
while( (*end == '0') && (discard-- > 0) )
{
*(end--) = 0;
size--;
}
}
else
{
while( *end == '0' )
{
*(end--) = 0;
size--;
}
}
}
return std::string( result, size );
}
STRING_SPRINTF
#define STRING_SPRINTF
Definition: Utility.h:196

## convert() [2/3]

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

inline static

Definition at line 463 of file FieldConvertors.h.

References IS_DIGIT.

{
const char * i = value.c_str();
// Catch null strings
if( !*i ) return false;
// Eat leading '-' and recheck for null string
if( *i == '-' && !*++i ) return false;
bool haveDigit = false;
if( IS_DIGIT(*i) )
{
haveDigit = true;
while( IS_DIGIT (*++i) );
}
if( *i == '.' && IS_DIGIT(*++i) )
{
haveDigit = true;
while( IS_DIGIT (*++i) );
}
if( *i || !haveDigit ) return false;
result = fast_atof( value.c_str() );
return true;
}
IS_DIGIT
#define IS_DIGIT(x)
Definition: FieldConvertors.h:43
FIX::DoubleConvertor::fast_atof
static double fast_atof(const char *p)
Definition: FieldConvertors.h:316

## convert() [3/3]

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

inline static

Definition at line 492 of file FieldConvertors.h.

References FIX::EmptyConvertor::convert().

{
double result = 0.0;
if( !convert( value, result ) )
throw FieldConvertError(value);
else
return result;
}
FIX::DoubleConvertor::convert
static std::string convert(double value, int padding=0)
Definition: FieldConvertors.h:401

## fast_atof()

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

inline static private

Definition at line 316 of file FieldConvertors.h.

References IS_DIGIT, and IS_SPACE.

{
bool frac(false);
double sign(1.), value(0.), scale(1.);
while (IS_SPACE(*p))
++p;
// Get sign, if any.
if (*p == '-')
{
sign = -1.;
++p;
}
else if (*p == '+')
++p;
// Get digits before decimal point or exponent, if any.
while (IS_DIGIT(*p))
{
value = value * 10. + (*p - '0');
++p;
}
// Get digits after decimal point, if any.
if (*p == '.')
{
++p;
double pow10(10.);
while (IS_DIGIT(*p))
{
value += (*p - '0') / pow10;
pow10 *= 10.;
++p;
}
}
// Handle exponent, if any.
if (toupper(*p) == 'E')
{
unsigned int expon(0);
++p;
// Get sign of exponent, if any.
if (*p == '-')
{
frac = true;
++p;
}
else if (*p == '+')
++p;
// Get digits of exponent, if any.
while (IS_DIGIT(*p))
{
expon = expon * 10 + (*p - '0');
++p;
}
if (expon > 308)
expon = 308;
// Calculate scaling factor.
while (expon >= 50)
{
scale *= 1E50;
expon -= 50;
}
while (expon >= 8)
{
scale *= 1E8;
expon -= 8;
}
while (expon > 0)
{
scale *= 10.0;
expon -= 1;
}
}
// Return signed and scaled floating point result.
return sign * (frac ? (value / scale) : (value * scale));
}
IS_DIGIT
#define IS_DIGIT(x)
Definition: FieldConvertors.h:43
IS_SPACE
#define IS_SPACE(x)
Definition: FieldConvertors.h:42

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

- FieldConvertors.h