From 6c9c0ba02b61192fa27032112c61d309733a2256 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Apr 2021 20:00:31 +0100 Subject: [PATCH] Determine number of digits correctly when increment > 1 Using increment greater than 1 but with a fractional part should still work, i.e. should use enough digits to fully show the fractional part of the numbers that can be obtained by using this increment. --- src/generic/spinctlg.cpp | 4 +++- tests/controls/spinctrldbltest.cpp | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/generic/spinctlg.cpp b/src/generic/spinctlg.cpp index f9749f052b..830f64f6f0 100644 --- a/src/generic/spinctlg.cpp +++ b/src/generic/spinctlg.cpp @@ -805,8 +805,10 @@ void wxSpinCtrlDouble::ResetTextValidator() /* static */ unsigned wxSpinCtrlDouble::DetermineDigits(double inc) { + // TODO-C++11: Use std::modf() to get the fractional part. inc = fabs(inc); - if ( inc > 0.0 && inc < 1.0 ) + inc -= static_cast(inc); + if ( inc > 0.0 ) { return wxMin(SPINCTRLDBL_MAX_DIGITS, -static_cast(floor(log10(inc)))); } diff --git a/tests/controls/spinctrldbltest.cpp b/tests/controls/spinctrldbltest.cpp index e8a8f32459..c31e83aa84 100644 --- a/tests/controls/spinctrldbltest.cpp +++ b/tests/controls/spinctrldbltest.cpp @@ -236,6 +236,12 @@ TEST_CASE_METHOD(SpinCtrlDoubleTestCase, m_spin->SetIncrement(0.001); m_spin->SetValue(1.23456789); CHECK( m_spin->GetTextValue() == "1.23457" ); + + // Check that using increment greater than 1 also works. + m_spin->SetDigits(0); + m_spin->SetIncrement(2.5); + m_spin->SetValue(7.5); + CHECK( m_spin->GetTextValue() == "7.5" ); } static inline unsigned int GetInitialDigits(double inc)