Skip to content

Commit 0b35147

Browse files
committed
Fields: be permissive when parsing formatted numbers
1 parent ba4fb65 commit 0b35147

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

src/core/qgsfield.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ bool QgsField::convertCompatible( QVariant &v ) const
285285
if ( !tmp.convert( d->type ) )
286286
{
287287
// This might be a string with thousand separator: use locale to convert
288-
bool ok;
289-
double d = QLocale().toDouble( v.toString(), &ok );
288+
bool ok = false;
289+
double d = qgsPermissiveToDouble( v.toString(), ok );
290290
if ( ok )
291291
{
292292
v = QVariant( d );
@@ -295,7 +295,7 @@ bool QgsField::convertCompatible( QVariant &v ) const
295295
// For not 'dot' locales, we also want to accept '.'
296296
if ( QLocale().decimalPoint() != '.' )
297297
{
298-
d = QLocale( QLocale::English ).toDouble( v.toString(), &ok );
298+
d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
299299
if ( ok )
300300
{
301301
v = QVariant( d );
@@ -313,7 +313,7 @@ bool QgsField::convertCompatible( QVariant &v ) const
313313
{
314314
// This might be a string with thousand separator: use locale to convert
315315
bool ok;
316-
int i = QLocale().toInt( v.toString(), &ok );
316+
int i = qgsPermissiveToInt( v.toString(), ok );
317317
if ( ok )
318318
{
319319
v = QVariant( i );
@@ -330,7 +330,7 @@ bool QgsField::convertCompatible( QVariant &v ) const
330330
{
331331
// This might be a string with thousand separator: use locale to convert
332332
bool ok;
333-
qlonglong l = QLocale().toLongLong( v.toString(), &ok );
333+
qlonglong l = qgsPermissiveToLongLong( v.toString(), ok );
334334
if ( ok )
335335
{
336336
v = QVariant( l );

tests/src/core/testqgsfield.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,14 @@ void TestQgsField::convertCompatible()
607607
QCOMPARE( stringDouble.type(), QVariant::Double );
608608
QCOMPARE( stringDouble, QVariant( 1223456.012345 ) );
609609

610+
// Test that wrongly formatted decimal separator are also accepted
611+
QLocale::setDefault( QLocale::German );
612+
QCOMPARE( QLocale().groupSeparator(), '.' );
613+
QCOMPARE( QLocale().decimalPoint(), ',' );
614+
stringDouble = QVariant( "12.23.456,012345" );
615+
QVERIFY( doubleField.convertCompatible( stringDouble ) );
616+
QCOMPARE( stringDouble.type(), QVariant::Double );
617+
QCOMPARE( stringDouble, QVariant( 1223456.012345 ) );
610618

611619
}
612620

0 commit comments

Comments
 (0)