| 1892 | |
| 1893 | template <unsigned Digits10, class ExponentType, class Allocator> |
| 1894 | std::string cpp_dec_float<Digits10, ExponentType, Allocator>::str(std::intmax_t number_of_digits, std::ios_base::fmtflags f) const |
| 1895 | { |
| 1896 | if ((this->isinf)()) |
| 1897 | { |
| 1898 | if (this->isneg()) |
| 1899 | return "-inf"; |
| 1900 | else if (f & std::ios_base::showpos) |
| 1901 | return "+inf"; |
| 1902 | else |
| 1903 | return "inf"; |
| 1904 | } |
| 1905 | else if ((this->isnan)()) |
| 1906 | { |
| 1907 | return "nan"; |
| 1908 | } |
| 1909 | |
| 1910 | std::string str; |
| 1911 | std::intmax_t org_digits(number_of_digits); |
| 1912 | exponent_type my_exp = order(); |
| 1913 | |
| 1914 | if (!(f & std::ios_base::fixed) && (number_of_digits == 0)) |
| 1915 | number_of_digits = cpp_dec_float_max_digits10; |
| 1916 | |
| 1917 | if (f & std::ios_base::fixed) |
| 1918 | { |
| 1919 | number_of_digits += my_exp + 1; |
| 1920 | } |
| 1921 | else if (f & std::ios_base::scientific) |
| 1922 | ++number_of_digits; |
| 1923 | // Determine the number of elements needed to provide the requested digits from cpp_dec_float<Digits10, ExponentType, Allocator>. |
| 1924 | const std::size_t number_of_elements = (std::min)(static_cast<std::size_t>((number_of_digits / static_cast<std::size_t>(cpp_dec_float_elem_digits10)) + 2u), |
| 1925 | static_cast<std::size_t>(cpp_dec_float_elem_number)); |
| 1926 | |
| 1927 | // Extract the remaining digits from cpp_dec_float<Digits10, ExponentType, Allocator> after the decimal point. |
| 1928 | std::stringstream ss; |
| 1929 | ss.imbue(std::locale::classic()); |
| 1930 | ss << data[0]; |
| 1931 | // Extract all of the digits from cpp_dec_float<Digits10, ExponentType, Allocator>, beginning with the first data element. |
| 1932 | for (std::size_t i = static_cast<std::size_t>(1u); i < number_of_elements; i++) |
| 1933 | { |
| 1934 | ss << std::setw(static_cast<std::streamsize>(cpp_dec_float_elem_digits10)) |
| 1935 | << std::setfill(static_cast<char>('0')) |
| 1936 | << data[i]; |
| 1937 | } |
| 1938 | str += ss.str(); |
| 1939 | |
| 1940 | bool have_leading_zeros = false; |
| 1941 | |
| 1942 | if (number_of_digits == 0) |
| 1943 | { |
| 1944 | // We only get here if the output format is "fixed" and we just need to |
| 1945 | // round the first non-zero digit. |
| 1946 | number_of_digits -= my_exp + 1; // reset to original value |
| 1947 | if (number_of_digits) |
| 1948 | { |
| 1949 | str.insert(static_cast<std::string::size_type>(0), std::string::size_type(number_of_digits), '0'); |
| 1950 | have_leading_zeros = true; |
| 1951 | } |