Skip to content

Commit

Permalink
Use QLocale when representing double fields
Browse files Browse the repository at this point in the history
  • Loading branch information
elpaso committed Jun 9, 2018
1 parent 289032b commit 3e44db5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
27 changes: 25 additions & 2 deletions src/core/qgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

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

/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
Expand Down Expand Up @@ -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();
}
Expand Down
15 changes: 12 additions & 3 deletions tests/src/core/testqgsfield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QObject>
#include <QString>
#include <QStringList>
#include <QLocale>

#include <memory>

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

void TestQgsField::init()
{

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

void TestQgsField::cleanup()
{

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

void TestQgsField::create()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 3e44db5

Please sign in to comment.