Skip to content
Permalink
Browse files

Fields: be permissive when parsing formatted numbers

  • Loading branch information
elpaso committed Sep 1, 2018
1 parent ba4fb65 commit 0b35147a92980e4f1b3d9d0be672d2302191f3fa
Showing with 13 additions and 5 deletions.
  1. +5 −5 src/core/qgsfield.cpp
  2. +8 −0 tests/src/core/testqgsfield.cpp
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 );
@@ -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 ) );

}

0 comments on commit 0b35147

Please sign in to comment.
You can’t perform that action at this time.