Skip to content

Commit 1d102b2

Browse files
committedApr 2, 2018
Never show int/long attributes in scientific notation
Fixes #18508, #18302 (cherry-picked from 054470f)
1 parent 842ec80 commit 1d102b2

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed
 

‎src/core/fieldformatter/qgsrangefieldformatter.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,24 @@ QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int field
6868
}
6969
}
7070
}
71-
else if ( field.type() == QVariant::Int &&
71+
else if ( ( field.type() == QVariant::Int ) &&
7272
value.isValid( ) )
7373
{
7474
bool ok;
7575
double val( value.toInt( &ok ) );
7676
if ( ok )
7777
{
78-
result = f_locale().toString( val );
78+
result = f_locale().toString( val, 'f', 0 );
79+
}
80+
}
81+
else if ( ( field.type() == QVariant::LongLong ) &&
82+
value.isValid( ) )
83+
{
84+
bool ok;
85+
double val( value.toLongLong( &ok ) );
86+
if ( ok )
87+
{
88+
result = f_locale().toString( val, 'f', 0 );
7989
}
8090
}
8191
else

‎src/core/qgsfield.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ QString QgsField::displayString( const QVariant &v ) const
205205
{
206206
if ( v.isNull() )
207207
{
208-
QgsSettings settings;
209208
return QgsApplication::nullRepresentation();
210209
}
211210

‎tests/src/core/testqgsfield.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,16 +307,33 @@ void TestQgsField::displayString()
307307
QVariant nullString = QVariant( QVariant::String );
308308
QCOMPARE( stringField.displayString( nullString ), QString( "TEST NULL" ) );
309309

310-
//test int value
310+
//test int value in string type
311311
QgsField intField( QStringLiteral( "int" ), QVariant::String, QStringLiteral( "int" ) );
312312
QCOMPARE( intField.displayString( 5 ), QString( "5" ) );
313+
QCOMPARE( intField.displayString( 599999898999LL ), QString( "599999898999" ) );
314+
315+
//test int value in int type
316+
QgsField intField2( QStringLiteral( "int" ), QVariant::Int, QStringLiteral( "int" ) );
317+
QCOMPARE( intField2.displayString( 5 ), QString( "5" ) );
318+
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) );
319+
320+
//test long type
321+
QgsField longField( QStringLiteral( "long" ), QVariant::LongLong, QStringLiteral( "longlong" ) );
322+
QCOMPARE( longField.displayString( 5 ), QString( "5" ) );
323+
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) );
324+
313325
//test NULL int
314326
QVariant nullInt = QVariant( QVariant::Int );
315327
QCOMPARE( intField.displayString( nullInt ), QString( "TEST NULL" ) );
316328

317329
//test double value
318330
QgsField doubleField( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10, 3 );
319331
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) );
332+
QgsField doubleFieldNoPrec( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10 );
333+
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) );
334+
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) );
335+
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );
336+
320337
//test NULL double
321338
QVariant nullDouble = QVariant( QVariant::Double );
322339
QCOMPARE( doubleField.displayString( nullDouble ), QString( "TEST NULL" ) );

‎tests/src/python/test_qgsfieldformatters.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
QgsValueMapFieldFormatter, QgsValueRelationFieldFormatter,
1919
QgsRelationReferenceFieldFormatter, QgsRangeFieldFormatter, QgsSettings)
2020

21+
from qgis.PyQt.QtCore import QCoreApplication
2122
from qgis.testing import start_app, unittest
2223

2324
start_app()
@@ -199,19 +200,33 @@ def test_representValue(self):
199200

200201
class TestQgsRangeFieldFormatter(unittest.TestCase):
201202

203+
@classmethod
204+
def setUpClass(cls):
205+
"""Run before all tests"""
206+
QCoreApplication.setOrganizationName("QGIS_Test")
207+
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsColorScheme.com")
208+
QCoreApplication.setApplicationName("QGIS_TestPyQgsColorScheme")
209+
QgsSettings().clear()
210+
start_app()
211+
202212
def test_representValue(self):
203213

204-
layer = QgsVectorLayer("point?field=int:integer&field=double:double",
214+
layer = QgsVectorLayer("point?field=int:integer&field=double:double&field=long:long",
205215
"layer", "memory")
206216
self.assertTrue(layer.isValid())
207217
QgsProject.instance().addMapLayers([layer])
208218

209219
fieldFormatter = QgsRangeFieldFormatter()
210220

211-
# Precision is ignored for integers
221+
# Precision is ignored for integers and longlongs
212222
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '123'), '123')
213223
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '123000'), '123000')
224+
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for integers!
214225
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, None), 'NULL')
226+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123'), '123')
227+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123000'), '123000')
228+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for long longs!
229+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, None), 'NULL')
215230

216231
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 1}, None, None), 'NULL')
217232
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 1}, None, '123'), '123.0')
@@ -235,6 +250,13 @@ def test_representValue(self):
235250
QgsSettings().setValue("locale/overrideFlag", True)
236251
QgsSettings().setValue("locale/userLocale", 'it')
237252

253+
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '9999999'),
254+
'9999999') # no scientific notation for integers!
255+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123'), '123')
256+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123000'), '123000')
257+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for long longs!
258+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, None), 'NULL')
259+
238260
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, None), 'NULL')
239261
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '123000'), '123000,00')
240262
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '0'), '0,00')

0 commit comments

Comments
 (0)
Please sign in to comment.