You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
956B

  1. #include "ui/Quantity.hpp"
  2. #include "string.hpp"
  3. namespace rack {
  4. namespace ui {
  5. int Quantity::getDisplayPrecision() {
  6. return 5;
  7. }
  8. std::string Quantity::getDisplayValueString() {
  9. return string::f("%.*g", getDisplayPrecision(), math::normalizeZero(getDisplayValue()));
  10. }
  11. void Quantity::setDisplayValueString(std::string s) {
  12. float v = 0.f;
  13. char suffix[2];
  14. int n = std::sscanf(s.c_str(), "%f%1s", &v, suffix);
  15. if (n >= 2) {
  16. // Parse SI prefixes
  17. switch (suffix[0]) {
  18. case 'n': v *= 1e-9f; break;
  19. case 'u': v *= 1e-6f; break;
  20. case 'm': v *= 1e-3f; break;
  21. case 'k': v *= 1e3f; break;
  22. case 'M': v *= 1e6f; break;
  23. case 'G': v *= 1e9f; break;
  24. default: break;
  25. }
  26. }
  27. if (n >= 1)
  28. setDisplayValue(v);
  29. }
  30. std::string Quantity::getString() {
  31. std::string s;
  32. std::string label = getLabel();
  33. if (!label.empty())
  34. s += label + ": ";
  35. s += getDisplayValueString() + getUnit();
  36. return s;
  37. }
  38. } // namespace ui
  39. } // namespace rack