Skip to content

Commit

Permalink
Don't show a decimal for zero-precision values in edge cases
Browse files Browse the repository at this point in the history
Previously, if the value was close to zero, we would show a single
decimal of precision, so that the user could be aware that there
was SOME power draw or feed.

Now, assume that sub-one-watt values are just error, or not worth
showing, and simply use zero-decimal-precision for all such values.

Also, we previously showed a decimal of precision for Percentage
values close to 100%, but now we just round up to 100%.

Contributes to #1328
  • Loading branch information
chriadam committed Jul 18, 2024
1 parent eb19a19 commit d56eea0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
32 changes: 12 additions & 20 deletions src/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ quantityInfo Units::getDisplayTextWithHysteresis(VenusOS::Enums::Units_Type unit
}
}

auto numberOfDigits = [](int value) {
auto numberOfDigits = [](int value) -> int {
int digits = 0;
while (value) {
value /= 10;
Expand All @@ -306,28 +306,20 @@ quantityInfo Units::getDisplayTextWithHysteresis(VenusOS::Enums::Units_Type unit
precision = 0;
}

// If value is between -1 and 1, but is not zero, show one decimal precision regardless of
// precision parameter, to avoid showing just '0'.
// And if showing percentages, avoid showing '100%' if value is between 99-100.
// if the scaled value is large (hundreds or thousands) no need to display all fractional digits after the decimal point.
// Possibly clip the precision by 1 or 2 fractional digits for scaled values between 10 and 99,
// except for Units_Volt_DC values i.e. don't clip precision for values like 53.35 V DC.
precision = precision < 0 ? defaultUnitPrecision(unit) : precision;
if (precision < 2 && (scaledValue != 0 && qAbs(scaledValue) < 1)
|| (quantity.unit.compare(QStringLiteral("%")) == 0 && scaledValue > 99 && scaledValue < 100)) {

int vFixed = qRound(scaledValue * 10);
scaledValue = (1.0*vFixed) / 10.0;
quantity.number = formattingLocale()->toString(scaledValue, 'f', 1);
} else {
// if the value is large (hundreds or thousands) no need to display decimals after the decimal point
int digits = numberOfDigits(scaledValue);
if (unit != VenusOS::Enums::Units_Volt_DC || digits > 2) // don't clip precision for values like 53.35 V DC.
precision = qMax(0, precision - qMax(0, digits - (precision == 1 ? 2 : 1)));

const qreal vFixedMultiplier = std::pow(10, precision);
int vFixed = qRound(scaledValue * vFixedMultiplier);
scaledValue = (1.0*vFixed) / vFixedMultiplier;
quantity.number = formattingLocale()->toString(scaledValue, 'f', precision);
int digits = numberOfDigits(static_cast<int>(scaledValue));
if (unit != VenusOS::Enums::Units_Volt_DC || digits > 2) {
precision = qMax(0, precision - qMax(0, digits - (precision == 1 ? 2 : 1)));
}

const qreal vFixedMultiplier = std::pow(10, precision);
int vFixed = qRound(scaledValue * vFixedMultiplier);
scaledValue = (1.0*vFixed) / vFixedMultiplier;
quantity.number = formattingLocale()->toString(scaledValue, 'f', precision);

return quantity;
}

Expand Down
14 changes: 7 additions & 7 deletions tests/units/tst_units.qml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ TestCase {
function test_percentage() {
expect(VenusOS.Units_Percentage, NaN, "--", "%")
expect(VenusOS.Units_Percentage, 0, "0", "%")
expect(VenusOS.Units_Percentage, 0.4, "0.4", "%")
expect(VenusOS.Units_Percentage, 0.55, "0.6", "%")
expect(VenusOS.Units_Percentage, 0.4, "0", "%")
expect(VenusOS.Units_Percentage, 0.55, "1", "%")
expect(VenusOS.Units_Percentage, 14, "14", "%")
expect(VenusOS.Units_Percentage, 15.5, "16", "%")
expect(VenusOS.Units_Percentage, 99.3, "99.3", "%")
expect(VenusOS.Units_Percentage, 99.7, "99.7", "%")
expect(VenusOS.Units_Percentage, 99.3, "99", "%")
expect(VenusOS.Units_Percentage, 99.7, "100", "%")
expect(VenusOS.Units_Percentage, 100, "100", "%")
}

Expand All @@ -60,8 +60,8 @@ TestCase {

expect(unit, NaN, "--", unitString)
expect(unit, 0, "0", unitString)
expect(unit, 0.4, "0.4", unitString)
expect(unit, 0.55, "0.6", unitString)
expect(unit, 0.4, "0", unitString)
expect(unit, 0.55, "1", unitString)
expect(unit, 14, "14", unitString)
expect(unit, 15.5, "16", unitString)
expect(unit, 100, "100", unitString)
Expand Down Expand Up @@ -152,7 +152,7 @@ TestCase {

expect(unit, NaN, "--", "kWh")
expect(unit, 0, "0", "kWh")
expect(unit, 0.0005, "0.5", "Wh")
expect(unit, 0.0005, "1", "Wh") // precision of three for kWh means precision of zero for Wh.
expect(unit, 0.005, "5", "Wh")
expect(unit, 0.3458, "346", "Wh")
expect(unit, 0.5, "500", "Wh")
Expand Down

0 comments on commit d56eea0

Please sign in to comment.