Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Localize numbers in expression preview
Fixes #43681
  • Loading branch information
elpaso authored and nyalldawson committed Jun 13, 2021
1 parent 02f21d4 commit 2fff67a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -1085,6 +1085,44 @@ QString QgsExpression::formatPreviewString( const QVariant &value, const bool ht
listStr += QLatin1Char( ']' );
return listStr;
}
else if ( value.type() == QVariant::Int || value.type() == QVariant::UInt || value.type() == QVariant::LongLong || value.type() == QVariant::ULongLong )
{
bool ok;
QString res;

if ( value.type() == QVariant::ULongLong )
{
res = QLocale().toString( value.toULongLong( &ok ) );
}
else
{
res = QLocale().toString( value.toLongLong( &ok ) );
}

if ( ok )
{
return res;
}
else
{
return value.toString();
}
}
// Qt madness with QMetaType::Float :/
else if ( value.type() == QVariant::Double || value.type() == static_cast<QVariant::Type>( QMetaType::Float ) )
{
bool ok;
const QString res { QLocale().toString( value.toDouble( &ok ), 'f', 6 ) };

if ( ok )
{
return res;
}
else
{
return value.toString();
}
}
else
{
return value.toString();
Expand Down
35 changes: 35 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -3957,6 +3957,41 @@ class TestQgsExpression: public QObject
QStringLiteral( "[ 'One', 'Two', 'A very long string that is going to be tr… ]" ) );
}

void test_formatPreviewStringWithLocale()
{
const QVariant t_int { 12345 };
QVariant t_uint( QVariant::UInt );
t_uint = 12345;
QVariant t_long( QVariant::LongLong );
t_long = 12345;
QVariant t_ulong( QVariant::ULongLong );
t_ulong = 12345;
const QVariant t_float { 12345.001F };
const QVariant t_double { 12345.001 };

QLocale().setDefault( QLocale::English );

QCOMPARE( QgsExpression::formatPreviewString( t_int ), QStringLiteral( "12,345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_uint ), QStringLiteral( "12,345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_long ), QStringLiteral( "12,345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_ulong ), QStringLiteral( "12,345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_float ), QStringLiteral( "12,345.000977" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_double ), QStringLiteral( "12,345.001000" ) );

QLocale().setDefault( QLocale::Italian );

QCOMPARE( QgsExpression::formatPreviewString( t_int ), QStringLiteral( "12.345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_uint ), QStringLiteral( "12.345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_long ), QStringLiteral( "12.345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_ulong ), QStringLiteral( "12.345" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_float ), QStringLiteral( "12.345,000977" ) );
QCOMPARE( QgsExpression::formatPreviewString( t_double ), QStringLiteral( "12.345,001000" ) );

QLocale().setDefault( QLocale::English );

}


void test_nowStatic()
{
QgsExpression e( QStringLiteral( "now()" ) );
Expand Down

0 comments on commit 2fff67a

Please sign in to comment.