I am trying to convert a double to string in c++. I would like to format it using a certain minimum amount of significant digits, taking into account machine epsilon.
As an example for 2 minimum significant decimals:
4 -> "4.00"
4.4 -> "4.40"
4.56434444 -> "4.56434444"
I would ideally like to imbue a stream with a locale and a numpunct facet
I currently am using something like
if(abs(floor((m_mes * 100) + 0.5) - (m_mes * 100)) > (std::numeric_limits<double>::epsilon() * 10000))
buffer << fixed << m_mes;
else
buffer << fixed << setprecision(2) << m_mes;
EDIT: The absolute value and rounding are a result of compensating for epsilon. The basic idea here is get floor(num * 100), subtract it from (num * 100). If the difference is not zero, then I use a precision higher than 2.
since my minimum significant digits is 2 I didn't want to slow the algorithm down to accept a variable amount.
Is there a way to do this with a numpunct facet? I am new to locales and have little experience with them. Or just a more simple way to accomplish what I have?