mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-25 15:21:54 +00:00
Add the 'n' float format specifier
This commit is contained in:
parent
f487ddbdfa
commit
c9d5a08ed8
@ -1097,6 +1097,9 @@ FMT_CONSTEXPR void handle_float_type_spec(char spec, Handler&& handler) {
|
||||
case 'A':
|
||||
handler.on_hex();
|
||||
break;
|
||||
case 'n':
|
||||
handler.on_num();
|
||||
break;
|
||||
default:
|
||||
handler.on_error();
|
||||
break;
|
||||
@ -1159,6 +1162,7 @@ class float_type_checker : private ErrorHandler {
|
||||
FMT_CONSTEXPR void on_fixed() {}
|
||||
FMT_CONSTEXPR void on_percent() {}
|
||||
FMT_CONSTEXPR void on_hex() {}
|
||||
FMT_CONSTEXPR void on_num() {}
|
||||
|
||||
FMT_CONSTEXPR void on_error() {
|
||||
ErrorHandler::on_error("invalid type specifier");
|
||||
@ -2642,9 +2646,14 @@ struct float_spec_handler {
|
||||
bool upper;
|
||||
bool fixed;
|
||||
bool as_percentage;
|
||||
bool use_locale;
|
||||
|
||||
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() {
|
||||
if (type == 'G') upper = true;
|
||||
@ -2668,6 +2677,8 @@ struct float_spec_handler {
|
||||
if (type == 'A') upper = true;
|
||||
}
|
||||
|
||||
void on_num() { use_locale = true; }
|
||||
|
||||
FMT_NORETURN void on_error() {
|
||||
FMT_THROW(format_error("invalid type specifier"));
|
||||
}
|
||||
@ -2728,6 +2739,7 @@ void internal::basic_writer<Range>::write_double(T value,
|
||||
} else if (spec.align() == ALIGN_DEFAULT) {
|
||||
as.align_ = ALIGN_RIGHT;
|
||||
}
|
||||
// TODO: add thousands separators if handler.use_locale is set
|
||||
if (use_grisu) {
|
||||
auto params = internal::gen_digits_params();
|
||||
params.fixed = handler.fixed;
|
||||
|
@ -1424,7 +1424,7 @@ TEST(FormatterTest, FormatFloat) {
|
||||
}
|
||||
|
||||
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.000000", format("{:f}", 0.0));
|
||||
EXPECT_EQ("0", format("{:g}", 0.0));
|
||||
@ -1447,6 +1447,10 @@ TEST(FormatterTest, FormatDouble) {
|
||||
EXPECT_EQ(buffer, format("{:A}", -42.0));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatDoubleLocale) {
|
||||
EXPECT_EQ("1.23", format("{:n}", 1.23));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, PrecisionRounding) {
|
||||
EXPECT_EQ("0", format("{:.0f}", 0.0));
|
||||
EXPECT_EQ("0", format("{:.0f}", 0.01));
|
||||
|
Loading…
Reference in New Issue
Block a user