Skip to content
Permalink
Browse files

Use QLocale when representing double fields

  • Loading branch information
elpaso committed Jun 5, 2018
1 parent 289032b commit 3e44db5a04e5f0134c557ac893c79253a850281d
Showing with 37 additions and 5 deletions.
  1. +25 −2 src/core/qgsfield.cpp
  2. +12 −3 tests/src/core/testqgsfield.cpp
@@ -22,6 +22,7 @@

#include <QDataStream>
#include <QIcon>
#include <QLocale>

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
@@ -208,8 +209,30 @@ QString QgsField::displayString( const QVariant &v ) const
return QgsApplication::nullRepresentation();
}

if ( d->type == QVariant::Double && d->precision > 0 )
return QString::number( v.toDouble(), 'f', d->precision );
if ( d->type == QVariant::Double )
{
if ( d->precision > 0 )
{
return QLocale().toString( v.toDouble(), 'f', d->precision );
}
else
{
// Precision is not set, let's guess it from the
// standard conversion to string
QString s( v.toString() );
int dotPosition( s.indexOf( '.' ) );
int precision;
if ( dotPosition < 0 )
{
precision = 0;
}
else
{
precision = s.length() - dotPosition - 1;
}
return QLocale().toString( v.toDouble(), 'f', precision );
}
}

return v.toString();
}
@@ -17,6 +17,7 @@
#include <QObject>
#include <QString>
#include <QStringList>
#include <QLocale>

#include <memory>

@@ -66,12 +67,12 @@ void TestQgsField::cleanupTestCase()

void TestQgsField::init()
{

QLocale::setDefault( QLocale::English );
}

void TestQgsField::cleanup()
{

QLocale::setDefault( QLocale::English );
}

void TestQgsField::create()
@@ -332,12 +333,20 @@ void TestQgsField::displayString()
QgsField doubleFieldNoPrec( QStringLiteral( "double" ), QVariant::Double, QStringLiteral( "double" ), 10 );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5.005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5.005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599999898999" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599,999,898,999" ) );

//test NULL double
QVariant nullDouble = QVariant( QVariant::Double );
QCOMPARE( doubleField.displayString( nullDouble ), QString( "TEST NULL" ) );

//test double value with German locale
QLocale::setDefault( QLocale::German );
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5,005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) );
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599.999.898.999" ) );


}

void TestQgsField::convertCompatible()

0 comments on commit 3e44db5

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