mirror of
https://github.com/fmtlib/fmt.git
synced 2025-11-02 23:21:45 +01:00
Add support for '%' type to output floating point values as a
percentage. This helps with compatibility with Python's format strings.
This commit is contained in:
@@ -1207,6 +1207,8 @@ TEST(FormatterTest, Precision) {
|
||||
"precision not allowed for this argument type");
|
||||
EXPECT_THROW_MSG(format("{0:.2f}", 42ull), format_error,
|
||||
"precision not allowed for this argument type");
|
||||
EXPECT_THROW_MSG(format("{0:.2%}", 42), format_error,
|
||||
"precision not allowed for this argument type");
|
||||
EXPECT_THROW_MSG(format("{0:3.0}", 'x'), format_error,
|
||||
"precision not allowed for this argument type");
|
||||
EXPECT_EQ("1.2", format("{0:.2}", 1.2345));
|
||||
@@ -1440,10 +1442,11 @@ TEST(FormatterTest, FormatConvertibleToLongLong) {
|
||||
|
||||
TEST(FormatterTest, FormatFloat) {
|
||||
EXPECT_EQ("392.500000", format("{0:f}", 392.5f));
|
||||
EXPECT_EQ("12.500000%", format("{0:%}", 0.125f));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatDouble) {
|
||||
check_unknown_types(1.2, "eEfFgGaA", "double");
|
||||
check_unknown_types(1.2, "eEfFgGaA%", "double");
|
||||
EXPECT_EQ("0.0", format("{:}", 0.0));
|
||||
EXPECT_EQ("0.000000", format("{:f}", 0.0));
|
||||
EXPECT_EQ("0", format("{:g}", 0.0));
|
||||
@@ -1452,6 +1455,8 @@ TEST(FormatterTest, FormatDouble) {
|
||||
EXPECT_EQ("392.65", format("{:G}", 392.65));
|
||||
EXPECT_EQ("392.650000", format("{:f}", 392.65));
|
||||
EXPECT_EQ("392.650000", format("{:F}", 392.65));
|
||||
EXPECT_EQ("12.500000%", format("{:%}", 0.125));
|
||||
EXPECT_EQ("12.34%", format("{:.2%}", 0.1234432));
|
||||
char buffer[BUFFER_SIZE];
|
||||
safe_sprintf(buffer, "%e", 392.65);
|
||||
EXPECT_EQ(buffer, format("{0:e}", 392.65));
|
||||
@@ -1473,6 +1478,7 @@ TEST(FormatterTest, FormatNaN) {
|
||||
EXPECT_EQ("nan ", format("{:<7}", nan));
|
||||
EXPECT_EQ(" nan ", format("{:^7}", nan));
|
||||
EXPECT_EQ(" nan", format("{:>7}", nan));
|
||||
EXPECT_EQ("nan%", format("{:%}", nan));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatInfinity) {
|
||||
@@ -1485,6 +1491,7 @@ TEST(FormatterTest, FormatInfinity) {
|
||||
EXPECT_EQ("inf ", format("{:<7}", inf));
|
||||
EXPECT_EQ(" inf ", format("{:^7}", inf));
|
||||
EXPECT_EQ(" inf", format("{:>7}", inf));
|
||||
EXPECT_EQ("inf%", format("{:%}", inf));
|
||||
}
|
||||
|
||||
TEST(FormatterTest, FormatLongDouble) {
|
||||
@@ -1495,6 +1502,8 @@ TEST(FormatterTest, FormatLongDouble) {
|
||||
EXPECT_EQ("392.65", format("{0:G}", 392.65l));
|
||||
EXPECT_EQ("392.650000", format("{0:f}", 392.65l));
|
||||
EXPECT_EQ("392.650000", format("{0:F}", 392.65l));
|
||||
EXPECT_EQ("12.500000%", format("{:%}", 0.125l));
|
||||
EXPECT_EQ("12.34%", format("{:.2%}", 0.1234432l));
|
||||
char buffer[BUFFER_SIZE];
|
||||
safe_sprintf(buffer, "%Le", 392.65l);
|
||||
EXPECT_EQ(buffer, format("{0:e}", 392.65l));
|
||||
|
||||
Reference in New Issue
Block a user