Lv2: Improve LED widget's digit calculation

This improves the way digits are calculated for display in
`Lv2ViewProc`:

- More than 2 digits are now recognized
- Minus signs are now recognized
- Tests have been added
This commit is contained in:
Johannes Lorenz
2023-09-22 23:58:52 +02:00
committed by Johannes Lorenz
parent c309d8bd11
commit d97377b640
4 changed files with 85 additions and 2 deletions

View File

@@ -325,6 +325,32 @@ static inline T absMin( T a, T b )
return std::abs(a) < std::abs(b) ? a : b;
}
// @brief Calculate number of digits which LcdSpinBox would show for a given number
// @note Once we upgrade to C++20, we could probably use std::formatted_size
static inline int numDigitsAsInt(float f)
{
// use rounding:
// LcdSpinBox sometimes uses roundf(), sometimes cast rounding
// we use rounding to be on the "safe side"
const float rounded = roundf(f);
int asInt = static_cast<int>(rounded);
int digits = 1; // always at least 1
if(asInt < 0)
{
++digits;
asInt = -asInt;
}
// "asInt" is positive from now
int32_t power = 1;
for(int32_t i = 1; i<10; ++i)
{
power *= 10;
if(static_cast<int32_t>(asInt) >= power) { ++digits; } // 2 digits for >=10, 3 for >=100
else { break; }
}
return digits;
}
} // namespace lmms