Percentage.h 986 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/Variant.h>
  9. #include <LibWeb/CSS/Angle.h>
  10. #include <LibWeb/CSS/Frequency.h>
  11. #include <LibWeb/CSS/Length.h>
  12. #include <LibWeb/CSS/Number.h>
  13. #include <LibWeb/CSS/Time.h>
  14. namespace Web::CSS {
  15. class Percentage {
  16. public:
  17. explicit Percentage(double value)
  18. : m_value(value)
  19. {
  20. }
  21. double value() const { return m_value; }
  22. double as_fraction() const { return m_value * 0.01; }
  23. String to_string() const
  24. {
  25. return MUST(String::formatted("{}%", m_value));
  26. }
  27. bool operator==(Percentage const& other) const { return m_value == other.m_value; }
  28. int operator<=>(Percentage const& other) const
  29. {
  30. if (m_value < other.m_value)
  31. return -1;
  32. if (m_value > other.m_value)
  33. return 1;
  34. return 0;
  35. }
  36. private:
  37. double m_value;
  38. };
  39. }