If you need to pass decimal values through an API which takes double
but you need to get the exact values, there isn't much of a problem: As long as you don't use more than std::numeric_limits<double>::digits10
digits, you can recover the original decimal value although not necessarily the same representation (trailing fractional zeros will be lost). To do so, you need to convert the original decimal string into the closest representation as double
and later use a suitable algorithm to restore the best decimal representation again. The parsing and formatting functions from the C and C++ standard libraries will do that correctly for you.
Note that you shouldn't try to do any arithmetic on the double
values when you want to restore the original decimal values: the result of double
arithmetic will use binary rounding and the values won't be the closest decimal values. However, as long as you only transfer the double
values, there is no problem.