BDE 4.39.x Production Release
Loading...
Searching...
No Matches
bslfmt_formatterfloating

Detailed Description

Provide a formatter customization for floating point types.

Outline

Purpose

Provide a formatter customization for floating point types

Classes

Canonical Header

bsl_format.h

Description

This component provides partial specializations of bsl::formatter catering for floating point types. The component defines a component-private bslfmt::FormatterFloating_Base class that implements the formatters for the float and double types, for both supported output character types (char and wchar_t). Partial specializations are then defined that use bslfmt::FormatterFloating_Base to specify the actual formatters in the bsl namespace. Notice that long double is not one of the supported types (see long double ).

This header is not intended to be included directly. Please include <bsl_format.h> to be able to use specializations of the bsl::formatter for floating point types.

long double

The type long double is rarely used in code as it is not only not guaranteed to provide more precision than double, but in fact it is exactly the same type as double on MSVC (Microsoft Windows), which makes any code that uses it for more precision non-portable.

We also do not have a ryu library implementation for long double so we could only use much slower means of "printing" it than the other two types.

For the above reasons long double is now not supported and an attempt to print it will result in a runtime error.

Usage

In this section we show the intended use of this component.

Example: Formatting a floating point number

We do not expect most users of bsl::format to interact with this type directly and instead use bsl::format or bsl::vformat, so this example is necessarily unrealistic.

Suppose we want to test pointer formatter's ability to format a number in hexadecimal format with defined alignment and padding.

mpc.advance_to(f.parse(mpc));
const double value = 42.24;
mfc.advance_to(bsl::as_const(f).format(value, mfc));
assert("1.51eb851eb851fp+5" == mfc.finalString());
Definition bslfmt_mockformatcontext.h:206
Definition bslfmt_mockparsecontext.h:291
BSLS_KEYWORD_CONSTEXPR bsl::add_const< TYPE >::type & as_const(TYPE &t) BSLS_KEYWORD_NOEXCEPT
Return a reference offering non-modifiable access to the specified t.
Definition bslstl_utility.h:129
Definition bslfmt_formatterbase.h:426