From a33bc107be33190a4ba7ed9da1034f51df1361dd Mon Sep 17 00:00:00 2001 From: John Jones-Steele Date: Fri, 23 Jul 2021 10:58:36 +0100 Subject: [PATCH 1/3] Fixed number truncation in deisplayed values Signed-off-by: John Jones-Steele --- .../AzQtComponents/Utilities/Conversions.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp index 4764b9f911..591b4a3701 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp @@ -43,19 +43,20 @@ namespace AzQtComponents { const QChar decimalPoint = locale.decimalPoint(); const QChar zeroDigit = locale.zeroDigit(); + const int numToStringDecimals = numDecimals < 8 ? 8 : numDecimals; - // We want to truncate, not round. toString will round, so we add an extra decimal place to the formatting - // so we can remove the last value - QString retValue = locale.toString(value, 'f', (numDecimals > 0) ? numDecimals + 1 : 0); + // We want to truncate, not round. toString will round, so we add extra decimal places to the formatting + // so we can remove the last values + QString retValue = locale.toString(value, 'f', (numDecimals > 0) ? numToStringDecimals : 0); // Handle special cases when we have decimals in our value if (numDecimals > 0) { // Truncate the extra digit now, if it's still there int decimalPointIndex = retValue.lastIndexOf(decimalPoint); - if ((decimalPointIndex > 0) && (retValue.size() - (decimalPointIndex + 1)) == (numDecimals + 1)) + if ((decimalPointIndex > 0) && (retValue.size() - (decimalPointIndex + 1)) == numToStringDecimals) { - retValue.resize(retValue.size() - 1); + retValue.resize(retValue.size() - (numToStringDecimals - numDecimals)); } // Remove trailing zeros, since the locale conversion won't do From b1922fe95e7096c5e35cba6f94c3dccdab5af49d Mon Sep 17 00:00:00 2001 From: John Jones-Steele Date: Fri, 23 Jul 2021 15:26:38 +0100 Subject: [PATCH 2/3] Changed comment to reflect PR Signed-off-by: John Jones-Steele --- .../AzQtComponents/AzQtComponents/Utilities/Conversions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp index 591b4a3701..c9327d199e 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp @@ -52,7 +52,7 @@ namespace AzQtComponents // Handle special cases when we have decimals in our value if (numDecimals > 0) { - // Truncate the extra digit now, if it's still there + // Truncate the extra digits now, if they're still there int decimalPointIndex = retValue.lastIndexOf(decimalPoint); if ((decimalPointIndex > 0) && (retValue.size() - (decimalPointIndex + 1)) == numToStringDecimals) { From 73706fa04e6d17a26ab4a7b24f8984d5228bb438 Mon Sep 17 00:00:00 2001 From: John Jones-Steele Date: Mon, 26 Jul 2021 16:10:21 +0100 Subject: [PATCH 3/3] Added Tests to check code is correct Signed-off-by: John Jones-Steele --- .../AzQtComponents/Utilities/Conversions.cpp | 2 +- .../AzToolsFramework/Tests/SpinBoxTests.cpp | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp index c9327d199e..bef41bb487 100644 --- a/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp +++ b/Code/Framework/AzQtComponents/AzQtComponents/Utilities/Conversions.cpp @@ -43,7 +43,7 @@ namespace AzQtComponents { const QChar decimalPoint = locale.decimalPoint(); const QChar zeroDigit = locale.zeroDigit(); - const int numToStringDecimals = numDecimals < 8 ? 8 : numDecimals; + const int numToStringDecimals = AZStd::max(numDecimals, 20); // We want to truncate, not round. toString will round, so we add extra decimal places to the formatting // so we can remove the last values diff --git a/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp b/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp index d1c3ee5b3c..eb09c68cee 100644 --- a/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp +++ b/Code/Framework/AzToolsFramework/Tests/SpinBoxTests.cpp @@ -77,6 +77,19 @@ namespace UnitTest m_intSpinBox.reset(); } + QString setupTruncationTest(QString textValue) + { + QString retval; + m_doubleSpinBoxWithLineEdit->setDecimals(7); + m_doubleSpinBoxWithLineEdit->setDisplayDecimals(3); + m_doubleSpinBoxWithLineEdit->setFocus(); + m_doubleSpinBoxWithLineEdit->GetLineEdit()->setText(textValue); + m_doubleSpinBoxWithLineEdit->clearFocus(); + + return m_doubleSpinBoxWithLineEdit->textFromValue(m_doubleSpinBoxWithLineEdit->value()); + } + + AZStd::unique_ptr m_dummyWidget; AZStd::unique_ptr m_intSpinBox; AZStd::unique_ptr m_doubleSpinBox; @@ -277,4 +290,34 @@ namespace UnitTest // test would result in a crash EXPECT_TRUE(m_intSpinBox.get() == nullptr); } + + TEST_F(SpinBoxFixture, SpinBoxCheckHighValueTruncatesCorrectly) + { + QString value = setupTruncationTest("0.9999999"); + + EXPECT_TRUE(value == "0.999"); + } + + TEST_F(SpinBoxFixture, SpinBoxCheckLowValueTruncatesCorrectly) + { + QString value = setupTruncationTest("0.0000001"); + + EXPECT_TRUE(value == "0.0"); + } + + TEST_F(SpinBoxFixture, SpinBoxCheckBugValuesTruncatesCorrectly) + { + QString value = setupTruncationTest("0.12395"); + + EXPECT_TRUE(value == "0.123"); + + value = setupTruncationTest("0.94496"); + + EXPECT_TRUE(value == "0.944"); + + value = setupTruncationTest("0.0009999"); + + EXPECT_TRUE(value == "0.0"); + } + } // namespace UnitTest