Fix float data conversion to string

Fixes precision digits, closing #430
Also avoids forcing a trailing '.0', closing #1627
This commit is contained in:
Sandro Santilli 2012-12-06 11:59:28 +01:00
parent 3d7575c282
commit 19522ac509

View file

@ -68,7 +68,11 @@ struct double_policy : boost::spirit::karma::real_policies<T>
{ {
typedef boost::spirit::karma::real_policies<T> base_type; typedef boost::spirit::karma::real_policies<T> base_type;
static int floatfield(T n) { return base_type::fmtflags::fixed; } static int floatfield(T n) { return base_type::fmtflags::fixed; }
static unsigned precision(T n) { return 16 ;} static unsigned precision(T n) { return 15 - trunc(log10(n)); }
template <typename OutputIterator>
static bool dot(OutputIterator& sink, T n, unsigned precision) {
return n ? *sink = '.', true : false;
}
}; };
@ -77,7 +81,7 @@ template <>
inline bool to_string(std::string & str, double value) inline bool to_string(std::string & str, double value)
{ {
namespace karma = boost::spirit::karma; namespace karma = boost::spirit::karma;
typedef boost::spirit::karma::real_generator<double, double_policy<double> > double_type; typedef karma::real_generator<double, double_policy<double> > double_type;
std::back_insert_iterator<std::string> sink(str); std::back_insert_iterator<std::string> sink(str);
return karma::generate(sink, double_type(), value); return karma::generate(sink, double_type(), value);
} }