1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:38:12 +00:00

AK: Add formatters for floating point numbers.

This commit is contained in:
asynts 2020-11-09 11:34:44 +01:00 committed by Andreas Kling
parent 3b3edbc4d2
commit 32957745fb
3 changed files with 132 additions and 1 deletions

View file

@ -235,4 +235,27 @@ TEST_CASE(file_descriptor)
fclose(file);
}
TEST_CASE(floating_point_numbers)
{
EXPECT_EQ(String::formatted("{}", 1.12), "1.120000");
EXPECT_EQ(String::formatted("{}", 1.), "1.000000");
EXPECT_EQ(String::formatted("{:.3}", 1.12), "1.120");
EXPECT_EQ(String::formatted("{:.1}", 1.12), "1.1");
EXPECT_EQ(String::formatted("{}", -1.12), "-1.120000");
// FIXME: There is always the question what we mean with the width field. Do we mean significant digits?
// Do we mean the whole width? This is what was the simplest to implement:
EXPECT_EQ(String::formatted("{:x>5.1}", 1.12), "xx1.1");
}
TEST_CASE(no_precision_no_trailing_number)
{
EXPECT_EQ(String::formatted("{:.0}", 0.1), "0.");
}
TEST_CASE(yay_this_implementation_sucks)
{
EXPECT_EQ(String::formatted("{:.0}", .99999999999), "0.");
}
TEST_MAIN(Format)