Commit c9d5a08e authored by Victor Zverovich's avatar Victor Zverovich

Add the 'n' float format specifier

parent f487ddbd
...@@ -1097,6 +1097,9 @@ FMT_CONSTEXPR void handle_float_type_spec(char spec, Handler&& handler) { ...@@ -1097,6 +1097,9 @@ FMT_CONSTEXPR void handle_float_type_spec(char spec, Handler&& handler) {
case 'A': case 'A':
handler.on_hex(); handler.on_hex();
break; break;
case 'n':
handler.on_num();
break;
default: default:
handler.on_error(); handler.on_error();
break; break;
...@@ -1159,6 +1162,7 @@ class float_type_checker : private ErrorHandler { ...@@ -1159,6 +1162,7 @@ class float_type_checker : private ErrorHandler {
FMT_CONSTEXPR void on_fixed() {} FMT_CONSTEXPR void on_fixed() {}
FMT_CONSTEXPR void on_percent() {} FMT_CONSTEXPR void on_percent() {}
FMT_CONSTEXPR void on_hex() {} FMT_CONSTEXPR void on_hex() {}
FMT_CONSTEXPR void on_num() {}
FMT_CONSTEXPR void on_error() { FMT_CONSTEXPR void on_error() {
ErrorHandler::on_error("invalid type specifier"); ErrorHandler::on_error("invalid type specifier");
...@@ -2642,9 +2646,14 @@ struct float_spec_handler { ...@@ -2642,9 +2646,14 @@ struct float_spec_handler {
bool upper; bool upper;
bool fixed; bool fixed;
bool as_percentage; bool as_percentage;
bool use_locale;
explicit float_spec_handler(char t) explicit float_spec_handler(char t)
: type(t), upper(false), fixed(false), as_percentage(false) {} : type(t),
upper(false),
fixed(false),
as_percentage(false),
use_locale(false) {}
void on_general() { void on_general() {
if (type == 'G') upper = true; if (type == 'G') upper = true;
...@@ -2668,6 +2677,8 @@ struct float_spec_handler { ...@@ -2668,6 +2677,8 @@ struct float_spec_handler {
if (type == 'A') upper = true; if (type == 'A') upper = true;
} }
void on_num() { use_locale = true; }
FMT_NORETURN void on_error() { FMT_NORETURN void on_error() {
FMT_THROW(format_error("invalid type specifier")); FMT_THROW(format_error("invalid type specifier"));
} }
...@@ -2728,6 +2739,7 @@ void internal::basic_writer<Range>::write_double(T value, ...@@ -2728,6 +2739,7 @@ void internal::basic_writer<Range>::write_double(T value,
} else if (spec.align() == ALIGN_DEFAULT) { } else if (spec.align() == ALIGN_DEFAULT) {
as.align_ = ALIGN_RIGHT; as.align_ = ALIGN_RIGHT;
} }
// TODO: add thousands separators if handler.use_locale is set
if (use_grisu) { if (use_grisu) {
auto params = internal::gen_digits_params(); auto params = internal::gen_digits_params();
params.fixed = handler.fixed; params.fixed = handler.fixed;
......
...@@ -1424,7 +1424,7 @@ TEST(FormatterTest, FormatFloat) { ...@@ -1424,7 +1424,7 @@ TEST(FormatterTest, FormatFloat) {
} }
TEST(FormatterTest, FormatDouble) { TEST(FormatterTest, FormatDouble) {
check_unknown_types(1.2, "eEfFgGaA%", "double"); check_unknown_types(1.2, "eEfFgGaAn%", "double");
EXPECT_EQ("0.0", format("{:}", 0.0)); EXPECT_EQ("0.0", format("{:}", 0.0));
EXPECT_EQ("0.000000", format("{:f}", 0.0)); EXPECT_EQ("0.000000", format("{:f}", 0.0));
EXPECT_EQ("0", format("{:g}", 0.0)); EXPECT_EQ("0", format("{:g}", 0.0));
...@@ -1447,6 +1447,10 @@ TEST(FormatterTest, FormatDouble) { ...@@ -1447,6 +1447,10 @@ TEST(FormatterTest, FormatDouble) {
EXPECT_EQ(buffer, format("{:A}", -42.0)); EXPECT_EQ(buffer, format("{:A}", -42.0));
} }
TEST(FormatterTest, FormatDoubleLocale) {
EXPECT_EQ("1.23", format("{:n}", 1.23));
}
TEST(FormatterTest, PrecisionRounding) { TEST(FormatterTest, PrecisionRounding) {
EXPECT_EQ("0", format("{:.0f}", 0.0)); EXPECT_EQ("0", format("{:.0f}", 0.0));
EXPECT_EQ("0", format("{:.0f}", 0.01)); EXPECT_EQ("0", format("{:.0f}", 0.01));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment