Skip to content
Permalink
Browse files

Never show int/long attributes in scientific notation

Fixes #18508, #18302
  • Loading branch information
nyalldawson committed Mar 26, 2018
1 parent cc7c935 commit 054470f032b5fe14e30d1c4c75931a4abbbdae2a
@@ -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
@@ -205,7 +205,6 @@ QString QgsField::displayString( const QVariant &v ) const
{
if ( v.isNull() )
{
QgsSettings settings;
return QgsApplication::nullRepresentation();
}

@@ -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" ) );
@@ -18,6 +18,7 @@
QgsValueMapFieldFormatter, QgsValueRelationFieldFormatter,
QgsRelationReferenceFieldFormatter, QgsRangeFieldFormatter, QgsSettings)

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

start_app()
@@ -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')
@@ -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')

0 comments on commit 054470f

Please sign in to comment.
You can’t perform that action at this time.