[charconv.to.chars]

# 28 Text processing library [[text]](./#text)

## 28.2 Primitive numeric conversions [[charconv]](charconv#to.chars)

### 28.2.2 Primitive numeric output conversion [charconv.to.chars]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L95)

All functions named to_chars convert value into a character string
by successively filling the range
[first, last),
where [first, last) is required to be a valid range[.](#1.sentence-1)

If the member ec of the return value
is such that the value
is equal to the value of a value-initialized errc,
the conversion was successful
and the member ptr is the one-past-the-end pointer of the characters written[.](#1.sentence-2)

Otherwise,
the member ec has the value errc​::​value_too_large,
the member ptr has the value last,
and the contents of the range [first, last) are unspecified[.](#1.sentence-3)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L113)

The functions that take a floating-point value but not a precision parameter
ensure that the string representation
consists of the smallest number of characters
such that
there is at least one digit before the radix point (if present) and
parsing the representation using the corresponding from_chars function
recovers value exactly[.](#2.sentence-1)

[*Note [1](#note-1)*:

This guarantee applies only ifto_chars and from_chars are executed on the same implementation[.](#2.sentence-2)

â *end note*]

If there are several such representations,
the representation with the smallest difference from
the floating-point argument value is chosen,
resolving any remaining ties using rounding according toround_to_nearest ([[round.style]](round.style "17.3.4 Enum float_­round_­style"))[.](#2.sentence-3)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L133)

The functions taking a chars_format parameter
determine the conversion specifier for printf as follows:
The conversion specifier isf if fmt is chars_format​::​fixed,e if fmt is chars_format​::​scientific,a (without leading "0x" in the result)
if fmt is chars_format​::​hex,
andg if fmt is chars_format​::​general[.](#3.sentence-1)

[ð](#lib:to_chars)

`constexpr to_chars_result to_chars(char* first, char* last, integer-type value, int base = 10);
`

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L150)

*Preconditions*: base has a value between 2 and 36 (inclusive)[.](#4.sentence-1)

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L154)

*Effects*: The value of value is converted
to a string of digits in the given base
(with no redundant leading zeroes)[.](#5.sentence-1)

Digits in the range 10..35 (inclusive)
are represented as lowercase characters a..z[.](#5.sentence-2)

If value is less than zero,
the representation starts with '-'[.](#5.sentence-3)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L164)

*Throws*: Nothing[.](#6.sentence-1)

[ð](#lib:to_chars_)

`to_chars_result to_chars(char* first, char* last, floating-point-type value);
`

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L175)

*Effects*: value is converted to a string
in the style of printf in the "C" locale[.](#7.sentence-1)

The conversion specifier is f or e,
chosen according to the requirement for a shortest representation
(see above);
a tie is resolved in favor of f[.](#7.sentence-2)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L185)

*Throws*: Nothing[.](#8.sentence-1)

[ð](#lib:to_chars__)

`to_chars_result to_chars(char* first, char* last, floating-point-type value, chars_format fmt);
`

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L196)

*Preconditions*: fmt has the value of
one of the enumerators of chars_format[.](#9.sentence-1)

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L201)

*Effects*: value is converted to a string
in the style of printf in the "C" locale[.](#10.sentence-1)

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L207)

*Throws*: Nothing[.](#11.sentence-1)

[ð](#lib:to_chars___)

`to_chars_result to_chars(char* first, char* last, floating-point-type value,
                         chars_format fmt, int precision);
`

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L219)

*Preconditions*: fmt has the value of
one of the enumerators of chars_format[.](#12.sentence-1)

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L224)

*Effects*: value is converted to a string
in the style of printf in the "C" locale
with the given precision[.](#13.sentence-1)

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/text.tex#L231)

*Throws*: Nothing[.](#14.sentence-1)

See also: ISO/IEC 9899:2024, 7.21.6.1
