Skip to content

Commit a85f67e

Browse files
committed
Skip custom handling of decimal point if it's a dot
1 parent f853c8d commit a85f67e

File tree

2 files changed

+81
-21
lines changed

2 files changed

+81
-21
lines changed

src/core/qgsfield.cpp

+40-19
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,48 @@ QString QgsField::displayString( const QVariant &v ) const
209209
return QgsApplication::nullRepresentation();
210210
}
211211

212+
// Special treatment for numeric types if group separator is set or decimalPoint is not a dot
212213
if ( d->type == QVariant::Double )
213214
{
214-
if ( d->precision > 0 )
215+
// Locales with decimal point != '.' or that require group separator: use QLocale
216+
if ( QLocale().decimalPoint() != '.' ||
217+
!( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
215218
{
216-
return QLocale().toString( v.toDouble(), 'f', d->precision );
217-
}
218-
else
219-
{
220-
// Precision is not set, let's guess it from the
221-
// standard conversion to string
222-
QString s( v.toString() );
223-
int dotPosition( s.indexOf( '.' ) );
224-
int precision;
225-
if ( dotPosition < 0 )
219+
if ( d->precision > 0 )
226220
{
227-
precision = 0;
221+
return QLocale().toString( v.toDouble(), 'f', d->precision );
228222
}
229223
else
230224
{
231-
precision = s.length() - dotPosition - 1;
225+
// Precision is not set, let's guess it from the
226+
// standard conversion to string
227+
QString s( v.toString() );
228+
int dotPosition( s.indexOf( '.' ) );
229+
int precision;
230+
if ( dotPosition < 0 )
231+
{
232+
precision = 0;
233+
}
234+
else
235+
{
236+
precision = s.length() - dotPosition - 1;
237+
}
238+
return QLocale().toString( v.toDouble(), 'f', precision );
232239
}
233-
return QLocale().toString( v.toDouble(), 'f', precision );
240+
}
241+
// Default for doubles with precision
242+
else if ( d->type == QVariant::Double && d->precision > 0 )
243+
{
244+
return QString::number( v.toDouble(), 'f', d->precision );
234245
}
235246
}
236-
247+
// Other numeric types out of doubles
248+
else if ( isNumeric() &&
249+
! QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator )
250+
{
251+
return QLocale().toString( v.toLongLong() );
252+
}
253+
// Fallback if special rules do not apply
237254
return v.toString();
238255
}
239256

@@ -281,11 +298,15 @@ bool QgsField::convertCompatible( QVariant &v ) const
281298
return true;
282299
}
283300

284-
// Give it a chance to convert to double since we accept both comma and dot as decimal point
285-
QVariant tmp( v );
286-
if ( d->type == QVariant::Double && !tmp.convert( d->type ) )
301+
// Give it a chance to convert to double since for not '.' locales
302+
// we accept both comma and dot as decimal point
303+
if ( d->type == QVariant::Double && QLocale().decimalPoint() != '.' )
287304
{
288-
v = v.toString().replace( ',', '.' );
305+
QVariant tmp( v );
306+
if ( d->type == QVariant::Double && !tmp.convert( d->type ) )
307+
{
308+
v = v.toString().replace( ',', '.' );
309+
}
289310
}
290311

291312
if ( !v.convert( d->type ) )

tests/src/core/testqgsfield.cpp

+41-2
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ void TestQgsField::displayString()
316316
//test int value in int type
317317
QgsField intField2( QStringLiteral( "int" ), QVariant::Int, QStringLiteral( "int" ) );
318318
QCOMPARE( intField2.displayString( 5 ), QString( "5" ) );
319-
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) );
319+
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599,999,898,999" ) );
320320

321321
//test long type
322322
QgsField longField( QStringLiteral( "long" ), QVariant::LongLong, QStringLiteral( "longlong" ) );
323323
QCOMPARE( longField.displayString( 5 ), QString( "5" ) );
324-
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) );
324+
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599,999,898,999" ) );
325325

326326
//test NULL int
327327
QVariant nullInt = QVariant( QVariant::Int );
@@ -333,6 +333,8 @@ void TestQgsField::displayString()
333333
QgsField doubleFieldNoPrec( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10 );
334334
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) );
335335
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) );
336+
QCOMPARE( QLocale().decimalPoint(), '.' );
337+
QCOMPARE( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator, QLocale::NumberOption::DefaultNumberOptions );
336338
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599,999,898,999" ) );
337339

338340
//test NULL double
@@ -345,6 +347,43 @@ void TestQgsField::displayString()
345347
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) );
346348
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) );
347349
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599.999.898.999" ) );
350+
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5.999,123456" ) );
351+
352+
//test value with custom German locale (OmitGroupSeparator)
353+
QLocale customGerman( QLocale::German );
354+
customGerman.setNumberOptions( QLocale::NumberOption::OmitGroupSeparator );
355+
QLocale::setDefault( customGerman );
356+
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5,005" ) );
357+
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) );
358+
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) );
359+
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );
360+
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5999,123456" ) );
361+
362+
//test int value in int type with custom German locale (OmitGroupSeparator)
363+
QCOMPARE( intField2.displayString( 5 ), QString( "5" ) );
364+
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) );
365+
366+
//test long type with custom German locale (OmitGroupSeparator)
367+
QCOMPARE( longField.displayString( 5 ), QString( "5" ) );
368+
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) );
369+
370+
//test value with custom english locale (OmitGroupSeparator)
371+
QLocale customEnglish( QLocale::English );
372+
customEnglish.setNumberOptions( QLocale::NumberOption::OmitGroupSeparator );
373+
QLocale::setDefault( customEnglish );
374+
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5.005" ) );
375+
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) );
376+
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) );
377+
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );
378+
QCOMPARE( doubleFieldNoPrec.displayString( 5999.123456 ), QString( "5999.123456" ) );
379+
380+
//test int value in int type with custom english locale (OmitGroupSeparator)
381+
QCOMPARE( intField2.displayString( 5 ), QString( "5" ) );
382+
QCOMPARE( intField2.displayString( 599999898999LL ), QString( "599999898999" ) );
383+
384+
//test long type with custom english locale (OmitGroupSeparator)
385+
QCOMPARE( longField.displayString( 5 ), QString( "5" ) );
386+
QCOMPARE( longField.displayString( 599999898999LL ), QString( "599999898999" ) );
348387

349388
}
350389

0 commit comments

Comments
 (0)