Skip to content

Commit

Permalink
Fields: be permissive when parsing formatted numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Sep 1, 2018
1 parent ba4fb65 commit 0b35147
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/core/qgsfield.cpp
Expand Up @@ -285,8 +285,8 @@ bool QgsField::convertCompatible( QVariant &v ) const
if ( !tmp.convert( d->type ) )
{
// This might be a string with thousand separator: use locale to convert
bool ok;
double d = QLocale().toDouble( v.toString(), &ok );
bool ok = false;
double d = qgsPermissiveToDouble( v.toString(), ok );
if ( ok )
{
v = QVariant( d );
Expand All @@ -295,7 +295,7 @@ bool QgsField::convertCompatible( QVariant &v ) const
// For not 'dot' locales, we also want to accept '.'
if ( QLocale().decimalPoint() != '.' )
{
d = QLocale( QLocale::English ).toDouble( v.toString(), &ok );
d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
if ( ok )
{
v = QVariant( d );
Expand All @@ -313,7 +313,7 @@ bool QgsField::convertCompatible( QVariant &v ) const
{
// This might be a string with thousand separator: use locale to convert
bool ok;
int i = QLocale().toInt( v.toString(), &ok );
int i = qgsPermissiveToInt( v.toString(), ok );
if ( ok )
{
v = QVariant( i );
Expand All @@ -330,7 +330,7 @@ bool QgsField::convertCompatible( QVariant &v ) const
{
// This might be a string with thousand separator: use locale to convert
bool ok;
qlonglong l = QLocale().toLongLong( v.toString(), &ok );
qlonglong l = qgsPermissiveToLongLong( v.toString(), ok );
if ( ok )
{
v = QVariant( l );
Expand Down
8 changes: 8 additions & 0 deletions tests/src/core/testqgsfield.cpp
Expand Up @@ -607,6 +607,14 @@ void TestQgsField::convertCompatible()
QCOMPARE( stringDouble.type(), QVariant::Double );
QCOMPARE( stringDouble, QVariant( 1223456.012345 ) );

// Test that wrongly formatted decimal separator are also accepted
QLocale::setDefault( QLocale::German );
QCOMPARE( QLocale().groupSeparator(), '.' );
QCOMPARE( QLocale().decimalPoint(), ',' );
stringDouble = QVariant( "12.23.456,012345" );
QVERIFY( doubleField.convertCompatible( stringDouble ) );
QCOMPARE( stringDouble.type(), QVariant::Double );
QCOMPARE( stringDouble, QVariant( 1223456.012345 ) );

}

Expand Down

0 comments on commit 0b35147

Please sign in to comment.