Skip to content

Commit 054470f

Browse files
committed
Never show int/long attributes in scientific notation
Fixes #18508, #18302
1 parent cc7c935 commit 054470f

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

src/core/fieldformatter/qgsrangefieldformatter.cpp

+12-2
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

-1
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

+18-1
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

+24-2
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()
@@ -224,19 +225,33 @@ def test_representValue(self):
224225

225226
class TestQgsRangeFieldFormatter(unittest.TestCase):
226227

228+
@classmethod
229+
def setUpClass(cls):
230+
"""Run before all tests"""
231+
QCoreApplication.setOrganizationName("QGIS_Test")
232+
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsColorScheme.com")
233+
QCoreApplication.setApplicationName("QGIS_TestPyQgsColorScheme")
234+
QgsSettings().clear()
235+
start_app()
236+
227237
def test_representValue(self):
228238

229-
layer = QgsVectorLayer("point?field=int:integer&field=double:double",
239+
layer = QgsVectorLayer("point?field=int:integer&field=double:double&field=long:long",
230240
"layer", "memory")
231241
self.assertTrue(layer.isValid())
232242
QgsProject.instance().addMapLayers([layer])
233243

234244
fieldFormatter = QgsRangeFieldFormatter()
235245

236-
# Precision is ignored for integers
246+
# Precision is ignored for integers and longlongs
237247
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '123'), '123')
238248
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '123000'), '123000')
249+
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for integers!
239250
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, None), 'NULL')
251+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123'), '123')
252+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123000'), '123000')
253+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for long longs!
254+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, None), 'NULL')
240255

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

278+
self.assertEqual(fieldFormatter.representValue(layer, 0, {'Precision': 1}, None, '9999999'),
279+
'9999999') # no scientific notation for integers!
280+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123'), '123')
281+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '123000'), '123000')
282+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, '9999999'), '9999999') # no scientific notation for long longs!
283+
self.assertEqual(fieldFormatter.representValue(layer, 2, {'Precision': 1}, None, None), 'NULL')
284+
263285
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, None), 'NULL')
264286
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '123000'), '123000,00')
265287
self.assertEqual(fieldFormatter.representValue(layer, 1, {'Precision': 2}, None, '0'), '0,00')

0 commit comments

Comments
 (0)