<cstdio> Support

The following functions analogous to those from <cstdio> are provided:

#include <boost/decimal/cstdio.hpp>

namespace boost {
namespace decimal {

template <typename... Dec>
int snprintf(char* buffer, std::size_t buf_size, const char* format, Dec... value) noexcept;

template <typename... Dec>
int fprintf(std::FILE* buffer, const char* format, Dec... values) noexcept;

template <typename... Dec>
int printf(const char* format, Dec... values) noexcept;

} //namespace decimal
} //namespace boost

These functions are limited in their support to only decimal floating point types, and are not a complete replacement for their std:: equivalents. Like std::snprintf, snprintf writes as many characters as fit in the buffer, followed by a null terminator when buf_size is not 0, and returns the length of the full output. If a conversion has no value, the functions return -1 and set errno to EINVAL.

Type Modifiers

The type modifiers to be used are:

  • "H" for decimal32_t

  • "D" for decimal64_t

  • "DD" for decimal128_t

The remaining specifications of cstdio are the same as usual:

  • "g" or "G" for general format

  • "e" or "E" for scientific format

  • "f" or "F" for fixed format

  • "a" or "A" for hex format

The uppercase format will return with all applicable values in uppercase (e.g. 3.14E+02 vs 3.14e+02, and INF vs inf)

A conversion can also have the flags "-", "+", " " and "0", and a minimum field width, before the precision (e.g. "%+010.3Df"). "%%" writes a single "%". A conversion without a precision uses 6, but "a" and "A" without a precision print the exact value.

Examples

Here are some example formats:

  • "%Hg" will print a decimal32_t in general format

  • "%.3De" will print a decimal64_t in scientific format with 3 digits of precision (e.g. 1.234e+05)

  • "%.5DDA" will print a decimal128_t in hex format with 5 digits of precision and all letters will be capitalized (e.g. 1.F2C34P+02 vs 1.f2c34p+02)