Skip to content

Commit

Permalink
display() function of pixel class added
Browse files Browse the repository at this point in the history
  • Loading branch information
iarfen committed Aug 12, 2024
1 parent 9c8622a commit 5fda0e3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 8 additions & 3 deletions special_units/pixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,13 @@ namespace scifir
return scalar_unit(value, { dimension(dimension::PIXEL, prefix::NONE, dimension::NUMERATOR) });
}

string pixel::display(int number_of_decimals) const
{
ostringstream output;
output << display_float(value,number_of_decimals) << " px";
return output.str();
}

void pixel::initialize_from_string(const string& init_pixel)
{
if (init_pixel.substr(init_pixel.length() - 3,3) == " px")
Expand All @@ -188,9 +195,7 @@ namespace scifir

string to_string(const pixel& x)
{
ostringstream output;
output << display_float(x.get_value()) << " px";
return output.str();
return x.display();
}

bool is_pixel(const string& init_pixel)
Expand Down
2 changes: 2 additions & 0 deletions special_units/pixel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ namespace scifir

scalar_unit to_scalar_unit() const;

string display(int number_of_decimals = 2) const;

private:
float value;

Expand Down
6 changes: 6 additions & 0 deletions tests/special_units/test_pixel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ TEST_CASE("pixel class") {
CHECK(bool(a.to_scalar_unit() == scalar_unit("40 px")));
}

SECTION("display() function of pixel class")
{
pixel a(5.0f);
CHECK(a.display() == "5 px");
}

SECTION("to_string() function of pixel class")
{
pixel a(3.0f);
Expand Down

0 comments on commit 5fda0e3

Please sign in to comment.