Fields: be permissive when parsing formatted numbers

This commit is contained in:
Alessandro Pasotti 2018-09-01 17:59:56 +02:00
parent ba4fb65072
commit 0b35147a92
2 changed files with 13 additions and 5 deletions

View File

@ -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 );

View File

@ -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 ) );
}