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.

47 lines
920B

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