Don't emit trailing zero for consistency with std::format

This commit is contained in:
Victor Zverovich 2020-09-23 08:39:00 -07:00
parent 5fd89d50e4
commit 2d4fde3a2e
5 changed files with 28 additions and 29 deletions

View File

@ -1276,7 +1276,7 @@ template <typename Char> class float_writer {
// 1234e7 -> 12340000000[.0+] // 1234e7 -> 12340000000[.0+]
it = copy_str<Char>(digits_, digits_ + num_digits_, it); it = copy_str<Char>(digits_, digits_ + num_digits_, it);
it = std::fill_n(it, full_exp - num_digits_, static_cast<Char>('0')); it = std::fill_n(it, full_exp - num_digits_, static_cast<Char>('0'));
if (specs_.showpoint || specs_.precision < 0) { if (specs_.showpoint) {
*it++ = decimal_point_; *it++ = decimal_point_;
int num_zeros = specs_.precision - full_exp; int num_zeros = specs_.precision - full_exp;
if (num_zeros <= 0) { if (num_zeros <= 0) {

View File

@ -583,8 +583,8 @@ TEST(FormatterTest, LeftAlign) {
EXPECT_EQ("42 ", format("{0:<5}", 42ul)); EXPECT_EQ("42 ", format("{0:<5}", 42ul));
EXPECT_EQ("-42 ", format("{0:<5}", -42ll)); EXPECT_EQ("-42 ", format("{0:<5}", -42ll));
EXPECT_EQ("42 ", format("{0:<5}", 42ull)); EXPECT_EQ("42 ", format("{0:<5}", 42ull));
EXPECT_EQ("-42.0 ", format("{0:<7}", -42.0)); EXPECT_EQ("-42 ", format("{0:<5}", -42.0));
EXPECT_EQ("-42.0 ", format("{0:<7}", -42.0l)); EXPECT_EQ("-42 ", format("{0:<5}", -42.0l));
EXPECT_EQ("c ", format("{0:<5}", 'c')); EXPECT_EQ("c ", format("{0:<5}", 'c'));
EXPECT_EQ("abc ", format("{0:<5}", "abc")); EXPECT_EQ("abc ", format("{0:<5}", "abc"));
EXPECT_EQ("0xface ", format("{0:<8}", reinterpret_cast<void*>(0xface))); EXPECT_EQ("0xface ", format("{0:<8}", reinterpret_cast<void*>(0xface)));
@ -600,8 +600,8 @@ TEST(FormatterTest, RightAlign) {
EXPECT_EQ(" 42", format("{0:>5}", 42ul)); EXPECT_EQ(" 42", format("{0:>5}", 42ul));
EXPECT_EQ(" -42", format("{0:>5}", -42ll)); EXPECT_EQ(" -42", format("{0:>5}", -42ll));
EXPECT_EQ(" 42", format("{0:>5}", 42ull)); EXPECT_EQ(" 42", format("{0:>5}", 42ull));
EXPECT_EQ(" -42.0", format("{0:>7}", -42.0)); EXPECT_EQ(" -42", format("{0:>5}", -42.0));
EXPECT_EQ(" -42.0", format("{0:>7}", -42.0l)); EXPECT_EQ(" -42", format("{0:>5}", -42.0l));
EXPECT_EQ(" c", format("{0:>5}", 'c')); EXPECT_EQ(" c", format("{0:>5}", 'c'));
EXPECT_EQ(" abc", format("{0:>5}", "abc")); EXPECT_EQ(" abc", format("{0:>5}", "abc"));
EXPECT_EQ(" 0xface", format("{0:>8}", reinterpret_cast<void*>(0xface))); EXPECT_EQ(" 0xface", format("{0:>8}", reinterpret_cast<void*>(0xface)));
@ -621,8 +621,8 @@ TEST(FormatterTest, CenterAlign) {
EXPECT_EQ(" 42 ", format("{0:^5}", 42ul)); EXPECT_EQ(" 42 ", format("{0:^5}", 42ul));
EXPECT_EQ(" -42 ", format("{0:^5}", -42ll)); EXPECT_EQ(" -42 ", format("{0:^5}", -42ll));
EXPECT_EQ(" 42 ", format("{0:^5}", 42ull)); EXPECT_EQ(" 42 ", format("{0:^5}", 42ull));
EXPECT_EQ(" -42.0 ", format("{0:^7}", -42.0)); EXPECT_EQ(" -42 ", format("{0:^5}", -42.0));
EXPECT_EQ(" -42.0 ", format("{0:^7}", -42.0l)); EXPECT_EQ(" -42 ", format("{0:^5}", -42.0l));
EXPECT_EQ(" c ", format("{0:^5}", 'c')); EXPECT_EQ(" c ", format("{0:^5}", 'c'));
EXPECT_EQ(" abc ", format("{0:^6}", "abc")); EXPECT_EQ(" abc ", format("{0:^6}", "abc"));
EXPECT_EQ(" 0xface ", format("{0:^8}", reinterpret_cast<void*>(0xface))); EXPECT_EQ(" 0xface ", format("{0:^8}", reinterpret_cast<void*>(0xface)));
@ -640,8 +640,8 @@ TEST(FormatterTest, Fill) {
EXPECT_EQ("***42", format("{0:*>5}", 42ul)); EXPECT_EQ("***42", format("{0:*>5}", 42ul));
EXPECT_EQ("**-42", format("{0:*>5}", -42ll)); EXPECT_EQ("**-42", format("{0:*>5}", -42ll));
EXPECT_EQ("***42", format("{0:*>5}", 42ull)); EXPECT_EQ("***42", format("{0:*>5}", 42ull));
EXPECT_EQ("**-42.0", format("{0:*>7}", -42.0)); EXPECT_EQ("**-42", format("{0:*>5}", -42.0));
EXPECT_EQ("**-42.0", format("{0:*>7}", -42.0l)); EXPECT_EQ("**-42", format("{0:*>5}", -42.0l));
EXPECT_EQ("c****", format("{0:*<5}", 'c')); EXPECT_EQ("c****", format("{0:*<5}", 'c'));
EXPECT_EQ("abc**", format("{0:*<5}", "abc")); EXPECT_EQ("abc**", format("{0:*<5}", "abc"));
EXPECT_EQ("**0xface", format("{0:*>8}", reinterpret_cast<void*>(0xface))); EXPECT_EQ("**0xface", format("{0:*>8}", reinterpret_cast<void*>(0xface)));
@ -664,8 +664,8 @@ TEST(FormatterTest, PlusSign) {
EXPECT_EQ("+42", format("{0:+}", 42ll)); EXPECT_EQ("+42", format("{0:+}", 42ll));
EXPECT_THROW_MSG(format("{0:+}", 42ull), format_error, EXPECT_THROW_MSG(format("{0:+}", 42ull), format_error,
"format specifier requires signed argument"); "format specifier requires signed argument");
EXPECT_EQ("+42.0", format("{0:+}", 42.0)); EXPECT_EQ("+42", format("{0:+}", 42.0));
EXPECT_EQ("+42.0", format("{0:+}", 42.0l)); EXPECT_EQ("+42", format("{0:+}", 42.0l));
EXPECT_THROW_MSG(format("{0:+", 'c'), format_error, EXPECT_THROW_MSG(format("{0:+", 'c'), format_error,
"missing '}' in format string"); "missing '}' in format string");
EXPECT_THROW_MSG(format("{0:+}", 'c'), format_error, EXPECT_THROW_MSG(format("{0:+}", 'c'), format_error,
@ -688,8 +688,8 @@ TEST(FormatterTest, MinusSign) {
EXPECT_EQ("42", format("{0:-}", 42ll)); EXPECT_EQ("42", format("{0:-}", 42ll));
EXPECT_THROW_MSG(format("{0:-}", 42ull), format_error, EXPECT_THROW_MSG(format("{0:-}", 42ull), format_error,
"format specifier requires signed argument"); "format specifier requires signed argument");
EXPECT_EQ("42.0", format("{0:-}", 42.0)); EXPECT_EQ("42", format("{0:-}", 42.0));
EXPECT_EQ("42.0", format("{0:-}", 42.0l)); EXPECT_EQ("42", format("{0:-}", 42.0l));
EXPECT_THROW_MSG(format("{0:-", 'c'), format_error, EXPECT_THROW_MSG(format("{0:-", 'c'), format_error,
"missing '}' in format string"); "missing '}' in format string");
EXPECT_THROW_MSG(format("{0:-}", 'c'), format_error, EXPECT_THROW_MSG(format("{0:-}", 'c'), format_error,
@ -712,8 +712,8 @@ TEST(FormatterTest, SpaceSign) {
EXPECT_EQ(" 42", format("{0: }", 42ll)); EXPECT_EQ(" 42", format("{0: }", 42ll));
EXPECT_THROW_MSG(format("{0: }", 42ull), format_error, EXPECT_THROW_MSG(format("{0: }", 42ull), format_error,
"format specifier requires signed argument"); "format specifier requires signed argument");
EXPECT_EQ(" 42.0", format("{0: }", 42.0)); EXPECT_EQ(" 42", format("{0: }", 42.0));
EXPECT_EQ(" 42.0", format("{0: }", 42.0l)); EXPECT_EQ(" 42", format("{0: }", 42.0l));
EXPECT_THROW_MSG(format("{0: ", 'c'), format_error, EXPECT_THROW_MSG(format("{0: ", 'c'), format_error,
"missing '}' in format string"); "missing '}' in format string");
EXPECT_THROW_MSG(format("{0: }", 'c'), format_error, EXPECT_THROW_MSG(format("{0: }", 'c'), format_error,
@ -782,8 +782,8 @@ TEST(FormatterTest, ZeroFlag) {
EXPECT_EQ("00042", format("{0:05}", 42ul)); EXPECT_EQ("00042", format("{0:05}", 42ul));
EXPECT_EQ("-0042", format("{0:05}", -42ll)); EXPECT_EQ("-0042", format("{0:05}", -42ll));
EXPECT_EQ("00042", format("{0:05}", 42ull)); EXPECT_EQ("00042", format("{0:05}", 42ull));
EXPECT_EQ("-0042.0", format("{0:07}", -42.0)); EXPECT_EQ("-000042", format("{0:07}", -42.0));
EXPECT_EQ("-0042.0", format("{0:07}", -42.0l)); EXPECT_EQ("-000042", format("{0:07}", -42.0l));
EXPECT_THROW_MSG(format("{0:0", 'c'), format_error, EXPECT_THROW_MSG(format("{0:0", 'c'), format_error,
"missing '}' in format string"); "missing '}' in format string");
EXPECT_THROW_MSG(format("{0:05}", 'c'), format_error, EXPECT_THROW_MSG(format("{0:05}", 'c'), format_error,
@ -956,7 +956,7 @@ TEST(FormatterTest, Precision) {
EXPECT_EQ("123.", format("{:#.0f}", 123.0)); EXPECT_EQ("123.", format("{:#.0f}", 123.0));
EXPECT_EQ("1.23", format("{:.02f}", 1.234)); EXPECT_EQ("1.23", format("{:.02f}", 1.234));
EXPECT_EQ("0.001", format("{:.1g}", 0.001)); EXPECT_EQ("0.001", format("{:.1g}", 0.001));
EXPECT_EQ("1019666400.0", format("{}", 1019666432.0f)); EXPECT_EQ("1019666400", format("{}", 1019666432.0f));
EXPECT_THROW_MSG(format("{0:.2}", reinterpret_cast<void*>(0xcafe)), EXPECT_THROW_MSG(format("{0:.2}", reinterpret_cast<void*>(0xcafe)),
format_error, format_error,
@ -1243,7 +1243,7 @@ TEST(FormatterTest, FormatFloat) {
TEST(FormatterTest, FormatDouble) { TEST(FormatterTest, FormatDouble) {
check_unknown_types(1.2, "eEfFgGaAnL%", "double"); check_unknown_types(1.2, "eEfFgGaAnL%", "double");
EXPECT_EQ("0.0", format("{:}", 0.0)); EXPECT_EQ("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));
EXPECT_EQ("392.65", format("{:}", 392.65)); EXPECT_EQ("392.65", format("{:}", 392.65));
@ -1310,7 +1310,7 @@ TEST(FormatterTest, FormatInfinity) {
} }
TEST(FormatterTest, FormatLongDouble) { TEST(FormatterTest, FormatLongDouble) {
EXPECT_EQ("0.0", format("{0:}", 0.0l)); EXPECT_EQ("0", format("{0:}", 0.0l));
EXPECT_EQ("0.000000", format("{0:f}", 0.0l)); EXPECT_EQ("0.000000", format("{0:f}", 0.0l));
EXPECT_EQ("392.65", format("{0:}", 392.65l)); EXPECT_EQ("392.65", format("{0:}", 392.65l));
EXPECT_EQ("392.65", format("{0:g}", 392.65l)); EXPECT_EQ("392.65", format("{0:g}", 392.65l));

View File

@ -35,7 +35,7 @@ TEST(GrisuTest, Inf) {
EXPECT_EQ("-inf", fmt::format("{}", -inf)); EXPECT_EQ("-inf", fmt::format("{}", -inf));
} }
TEST(GrisuTest, Zero) { EXPECT_EQ("0.0", fmt::format("{}", 0.0)); } TEST(GrisuTest, Zero) { EXPECT_EQ("0", fmt::format("{}", 0.0)); }
TEST(GrisuTest, Round) { TEST(GrisuTest, Round) {
EXPECT_EQ("1.9156918820264798e-56", EXPECT_EQ("1.9156918820264798e-56",
@ -47,9 +47,9 @@ TEST(GrisuTest, Prettify) {
EXPECT_EQ("0.0001", fmt::format("{}", 1e-4)); EXPECT_EQ("0.0001", fmt::format("{}", 1e-4));
EXPECT_EQ("1e-05", fmt::format("{}", 1e-5)); EXPECT_EQ("1e-05", fmt::format("{}", 1e-5));
EXPECT_EQ("9.999e-05", fmt::format("{}", 9.999e-5)); EXPECT_EQ("9.999e-05", fmt::format("{}", 9.999e-5));
EXPECT_EQ("10000000000.0", fmt::format("{}", 1e10)); EXPECT_EQ("10000000000", fmt::format("{}", 1e10));
EXPECT_EQ("100000000000.0", fmt::format("{}", 1e11)); EXPECT_EQ("100000000000", fmt::format("{}", 1e11));
EXPECT_EQ("12340000000.0", fmt::format("{}", 1234e7)); EXPECT_EQ("12340000000", fmt::format("{}", 1234e7));
EXPECT_EQ("12.34", fmt::format("{}", 1234e-2)); EXPECT_EQ("12.34", fmt::format("{}", 1234e-2));
EXPECT_EQ("0.001234", fmt::format("{}", 1234e-6)); EXPECT_EQ("0.001234", fmt::format("{}", 1234e-6));
EXPECT_EQ("0.1", fmt::format("{}", 0.1f)); EXPECT_EQ("0.1", fmt::format("{}", 0.1f));

View File

@ -151,10 +151,9 @@ FMT_END_NAMESPACE
TEST(FormatTest, Complex) { TEST(FormatTest, Complex) {
std::string s = fmt::format("{}", std::complex<double>(1, 2)); std::string s = fmt::format("{}", std::complex<double>(1, 2));
// We might want to drop trailing zeros for consistency with to_chars. EXPECT_EQ(s, "(1+2i)");
EXPECT_EQ(s, "(1.0+2.0i)");
EXPECT_EQ(fmt::format("{:.2f}", std::complex<double>(1, 2)), "(1.00+2.00i)"); EXPECT_EQ(fmt::format("{:.2f}", std::complex<double>(1, 2)), "(1.00+2.00i)");
EXPECT_EQ(fmt::format("{:12}", std::complex<double>(1, 2)), " (1.0+2.0i)"); EXPECT_EQ(fmt::format("{:8}", std::complex<double>(1, 2)), " (1+2i)");
} }
#endif // FMT_STATIC_THOUSANDS_SEPARATOR #endif // FMT_STATIC_THOUSANDS_SEPARATOR

View File

@ -54,7 +54,7 @@ TEST(RangesTest, FormatTuple) {
TEST(RangesTest, JoinTuple) { TEST(RangesTest, JoinTuple) {
// Value tuple args // Value tuple args
std::tuple<char, int, float> t1 = std::make_tuple('a', 1, 2.0f); std::tuple<char, int, float> t1 = std::make_tuple('a', 1, 2.0f);
EXPECT_EQ("(a, 1, 2.0)", fmt::format("({})", fmt::join(t1, ", "))); EXPECT_EQ("(a, 1, 2)", fmt::format("({})", fmt::join(t1, ", ")));
// Testing lvalue tuple args // Testing lvalue tuple args
int x = 4; int x = 4;
@ -67,7 +67,7 @@ TEST(RangesTest, JoinTuple) {
// Single element tuple // Single element tuple
std::tuple<float> t4{4.0f}; std::tuple<float> t4{4.0f};
EXPECT_EQ("4.0", fmt::format("{}", fmt::join(t4, "/"))); EXPECT_EQ("4", fmt::format("{}", fmt::join(t4, "/")));
} }
TEST(RangesTest, JoinInitializerList) { TEST(RangesTest, JoinInitializerList) {