Skip to content

Commit

Permalink
Fix storing string representations of doubles in an int field
Browse files Browse the repository at this point in the history
results in NULL rather than converting value to int

(cherry-picked from 1c76b93)
  • Loading branch information
nyalldawson committed Nov 19, 2015
1 parent 3ffdb6c commit ca127f5
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <QSettings>
#include <QtCore/qmath.h>
#include "qgis.h"

#if 0
QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
Expand Down Expand Up @@ -143,6 +144,30 @@ bool QgsField::convertCompatible( QVariant& v ) const
return false;
}

//String representations of doubles in QVariant will return false to convert( QVariant::Int )
//work around this by first converting to double, and then checking whether the double is convertible to int
if ( mType == QVariant::Int && v.canConvert( QVariant::Double ) )
{
bool ok = false;
double dbl = v.toDouble( &ok );
if ( !ok )
{
//couldn't convert to number
v = QVariant( mType );
return false;
}

double round = qgsRound( dbl );
if ( round > INT_MAX || round < -INT_MAX )
{
//double too large to fit in int
v = QVariant( mType );
return false;
}
v = QVariant( qRound( dbl ) );
return true;
}

if ( !v.convert( mType ) )
{
v = QVariant( mType );
Expand Down

0 comments on commit ca127f5

Please sign in to comment.