Skip to content

Commit 3e44db5

Browse files
committed
Use QLocale when representing double fields
1 parent 289032b commit 3e44db5

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/core/qgsfield.cpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <QDataStream>
2424
#include <QIcon>
25+
#include <QLocale>
2526

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

211-
if ( d->type == QVariant::Double && d->precision > 0 )
212-
return QString::number( v.toDouble(), 'f', d->precision );
212+
if ( d->type == QVariant::Double )
213+
{
214+
if ( d->precision > 0 )
215+
{
216+
return QLocale().toString( v.toDouble(), 'f', d->precision );
217+
}
218+
else
219+
{
220+
// Precision is not set, let's guess it from the
221+
// standard conversion to string
222+
QString s( v.toString() );
223+
int dotPosition( s.indexOf( '.' ) );
224+
int precision;
225+
if ( dotPosition < 0 )
226+
{
227+
precision = 0;
228+
}
229+
else
230+
{
231+
precision = s.length() - dotPosition - 1;
232+
}
233+
return QLocale().toString( v.toDouble(), 'f', precision );
234+
}
235+
}
213236

214237
return v.toString();
215238
}

tests/src/core/testqgsfield.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <QObject>
1818
#include <QString>
1919
#include <QStringList>
20+
#include <QLocale>
2021

2122
#include <memory>
2223

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

6768
void TestQgsField::init()
6869
{
69-
70+
QLocale::setDefault( QLocale::English );
7071
}
7172

7273
void TestQgsField::cleanup()
7374
{
74-
75+
QLocale::setDefault( QLocale::English );
7576
}
7677

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

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

342+
//test double value with German locale
343+
QLocale::setDefault( QLocale::German );
344+
QCOMPARE( doubleField.displayString( 5.005005 ), QString( "5,005" ) );
345+
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005 ), QString( "5,005005" ) );
346+
QCOMPARE( doubleFieldNoPrec.displayString( 5.005005005 ), QString( "5,005005005" ) );
347+
QCOMPARE( doubleFieldNoPrec.displayString( 599999898999.0 ), QString( "599.999.898.999" ) );
348+
349+
341350
}
342351

343352
void TestQgsField::convertCompatible()

0 commit comments

Comments
 (0)