Skip to content

Commit

Permalink
Never show int/long attributes in scientific notation
Browse files Browse the repository at this point in the history
Fixes #18508, #18302
  • Loading branch information
nyalldawson committed Mar 28, 2018
1 parent cc7c935 commit 054470f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
14 changes: 12 additions & 2 deletions src/core/fieldformatter/qgsrangefieldformatter.cpp
Expand Up @@ -68,14 +68,24 @@ QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int field
}
}
}
else if ( field.type() == QVariant::Int &&
else if ( ( field.type() == QVariant::Int ) &&
value.isValid( ) )
{
bool ok;
double val( value.toInt( &ok ) );
if ( ok )
{
result = f_locale().toString( val );
result = f_locale().toString( val, 'f', 0 );
}
}
else if ( ( field.type() == QVariant::LongLong ) &&
value.isValid( ) )
{
bool ok;
double val( value.toLongLong( &ok ) );
if ( ok )
{
result = f_locale().toString( val, 'f', 0 );
}
}
else
Expand Down
1 change: 0 additions & 1 deletion src/core/qgsfield.cpp
Expand Up @@ -205,7 +205,6 @@ QString QgsField::displayString( const QVariant &v ) const
{
if ( v.isNull() )
{
QgsSettings settings;
return QgsApplication::nullRepresentation();
}

Expand Down
19 changes: 18 additions & 1 deletion tests/src/core/testqgsfield.cpp
Expand Up @@ -307,16 +307,33 @@ void TestQgsField::displayString()
QVariant nullString = QVariant( QVariant::String );
QCOMPARE( stringField.displayString( nullString ), QString( "TEST NULL" ) );

//test int value
//test int value in string type
QgsField intField( QStringLiteral( "int" ), QVariant::String, QStringLiteral( "int" ) );
QCOMPARE( intField.displayString( 5 ), QString( "5" ) );
QCOMPARE( intField.displayString( 599999898999LL ), QString( "599999898999" ) );

//test int value in int type
QgsField intField2( QStringLiteral( "int" ), QVariant::Int, QStringLiteral( "int" ) );
QCOMPARE( intField2.displayString( 5 ), QString( "5" ) );
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) );

//test long type
QgsField longField( QStringLiteral( "long" ), QVariant::LongLong, QStringLiteral( "longlong" ) );
QCOMPARE( longField.displayString( 5 ), QString( "5" ) );
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) );

//test NULL int
QVariant nullInt = QVariant( QVariant::Int );
QCOMPARE( intField.displayString( nullInt ), QString( "TEST NULL" ) );

//test double value
QgsField doubleField( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10, 3 );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) );
QgsField doubleFieldNoPrec( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10 );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );

//test NULL double
QVariant nullDouble = QVariant( QVariant::Double );
QCOMPARE( doubleField.displayString( nullDouble ), QString( "TEST NULL" ) );
Expand Down
26 changes: 24 additions & 2 deletions tests/src/python/test_qgsfieldformatters.py
Expand Up @@ -18,6 +18,7 @@
QgsValueMapFieldFormatter, QgsValueRelationFieldFormatter,
QgsRelationReferenceFieldFormatter, QgsRangeFieldFormatter, QgsSettings)

from qgis.PyQt.QtCore import QCoreApplication
from qgis.testing import start_app, unittest

start_app()
Expand Down Expand Up @@ -224,19 +225,33 @@ def test_representValue(self):

class TestQgsRangeFieldFormatter(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""Run before all tests"""
QCoreApplication.setOrganizationName("QGIS_Test")
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsColorScheme.com")
QCoreApplication.setApplicationName("QGIS_TestPyQgsColorScheme")
QgsSettings().clear()
start_app()

def test_representValue(self):

layer = QgsVectorLayer("point?field=int:integer&field=double:double",
layer = QgsVectorLayer("point?field=int:integer&field=double:double&field=long:long",
"layer", "memory")
self.assertTrue(layer.isValid())
QgsProject.instance().addMapLayers([layer])

fieldFormatter = QgsRangeFieldFormatter()

# Precision is ignored for integers
# Precision is ignored for integers and longlongs
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '123'), '123')
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '123000'), '123000')
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for integers!
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, None), 'NULL')
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123'), '123')
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123000'), '123000')
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for long longs!
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, None), 'NULL')

self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 1}, None, None), 'NULL')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 1}, None, '123'), '123.0')
Expand All @@ -260,6 +275,13 @@ def test_representValue(self):
QgsSettings().setValue("locale/overrideFlag", True)
QgsSettings().setValue("locale/userLocale", 'it')

self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '9999999'),
'9999999') # no scientific notation for integers!
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123'), '123')
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123000'), '123000')
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for long longs!
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, None), 'NULL')

self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, None), 'NULL')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '123000'), '123000,00')
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '0'), '0,00')
Expand Down

0 comments on commit 054470f

Please sign in to comment.