Browse Source

Add SI prefixes parsing to Quantity::setDisplayValue

tags/v1.0.0
Andrew Belt 6 years ago
parent
commit
a88a3a3f58
1 changed files with 17 additions and 4 deletions
  1. +17
    -4
      src/ui/Quantity.cpp

+ 17
- 4
src/ui/Quantity.cpp View File

@@ -14,10 +14,23 @@ std::string Quantity::getDisplayValueString() {
} }


void Quantity::setDisplayValueString(std::string s) { void Quantity::setDisplayValueString(std::string s) {
float displayValue = 0.f;
int n = std::sscanf(s.c_str(), "%f", &displayValue);
if (n == 1)
setDisplayValue(displayValue);
float v = 0.f;
char suffix[2];
int n = std::sscanf(s.c_str(), "%f%1s", &v, suffix);
if (n >= 2) {
// Parse SI prefixes
switch (suffix[0]) {
case 'n': v *= 1e-9f; break;
case 'u': v *= 1e-6f; break;
case 'm': v *= 1e-3f; break;
case 'k': v *= 1e3f; break;
case 'M': v *= 1e6f; break;
case 'G': v *= 1e9f; break;
default: break;
}
}
if (n >= 1)
setDisplayValue(v);
} }


std::string Quantity::getString() { std::string Quantity::getString() {


Loading…
Cancel
Save