Merge pull request #2387 from aws-lumberyard-dev/TruncateNumericalValuesCorrectly

Fixed number truncation in displayed values
This commit is contained in:
Terry Michaels
2021-07-26 11:34:58 -05:00
committed by GitHub
2 changed files with 50 additions and 6 deletions
@@ -43,19 +43,20 @@ namespace AzQtComponents
{
const QChar decimalPoint = locale.decimalPoint();
const QChar zeroDigit = locale.zeroDigit();
const int numToStringDecimals = AZStd::max(numDecimals, 20);
// 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
// Truncate the extra digits now, if they're 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
@@ -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<QWidget> m_dummyWidget;
AZStd::unique_ptr<AzQtComponents::SpinBox> m_intSpinBox;
AZStd::unique_ptr<AzQtComponents::DoubleSpinBox> 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