Skip to content

Commit cbd3068

Browse files
committedJun 5, 2020
[expression] Add optional language parameter to format_number() to control locale across different systems
1 parent 4fe0651 commit cbd3068

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed
 
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "format_number",
33
"type": "function",
4-
"description": "Returns a number formatted with the locale separator for thousands. Also truncates the number to the number of supplied places.",
4+
"description": "Returns a number formatted with the locale separator for thousands. Also truncates the decimal places to the number of supplied places.",
55
"arguments": [ {"arg":"number","description":"number to be formatted"},
6-
{"arg":"places","description":"integer representing the number of decimal places to truncate the string to."}],
6+
{"arg":"places","description":"integer representing the number of decimal places to truncate the string to."},
7+
{"arg":"language","optional":true,"description":"language (lowercase, two- or three-letter, ISO 639 language code) used to format the number into a string"}],
78
"examples": [ { "expression":"format_number(10000000.332,2)", "returns":"'10,000,000.33'"}
89
]
910
}

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4364,29 +4364,27 @@ static QVariant fcnPi( const QVariantList &values, const QgsExpressionContext *,
43644364

43654365
static QVariant fcnFormatNumber( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
43664366
{
4367-
double value = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
4368-
int places = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
4367+
const double value = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
4368+
const int places = QgsExpressionUtils::getNativeIntValue( values.at( 1 ), parent );
4369+
const QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
43694370
if ( places < 0 )
43704371
{
43714372
parent->setEvalErrorString( QObject::tr( "Number of places must be positive" ) );
43724373
return QVariant();
43734374
}
4374-
QLocale locale = QLocale();
4375+
4376+
QLocale locale = !language.isEmpty() ? QLocale( language ) : QLocale();
43754377
locale.setNumberOptions( locale.numberOptions() &= ~QLocale::NumberOption::OmitGroupSeparator );
43764378
return locale.toString( value, 'f', places );
43774379
}
43784380

43794381
static QVariant fcnFormatDate( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
43804382
{
4381-
QDateTime datetime = QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent );
4382-
QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4383-
QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
4383+
const QDateTime datetime = QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent );
4384+
const QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
4385+
const QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
43844386

4385-
QLocale locale = QLocale();
4386-
if ( !language.isEmpty() )
4387-
{
4388-
locale = QLocale( language );
4389-
}
4387+
QLocale locale = !language.isEmpty() ? QLocale( language ) : QLocale();
43904388
return locale.toString( datetime, format );
43914389
}
43924390

@@ -5879,7 +5877,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
58795877
<< new QgsStaticExpressionFunction( QStringLiteral( "rpad" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "width" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "fill" ) ), fcnRPad, QStringLiteral( "String" ) )
58805878
<< new QgsStaticExpressionFunction( QStringLiteral( "lpad" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "width" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "fill" ) ), fcnLPad, QStringLiteral( "String" ) )
58815879
<< new QgsStaticExpressionFunction( QStringLiteral( "format" ), -1, fcnFormatString, QStringLiteral( "String" ) )
5882-
<< new QgsStaticExpressionFunction( QStringLiteral( "format_number" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "number" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "places" ) ), fcnFormatNumber, QStringLiteral( "String" ) )
5880+
<< new QgsStaticExpressionFunction( QStringLiteral( "format_number" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "number" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "places" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "language" ), true, QVariant() ), fcnFormatNumber, QStringLiteral( "String" ) )
58835881
<< new QgsStaticExpressionFunction( QStringLiteral( "format_date" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "datetime" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "format" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "language" ), true, QVariant() ), fcnFormatDate, QStringList() << QStringLiteral( "String" ) << QStringLiteral( "Date and Time" ) )
58845882
<< new QgsStaticExpressionFunction( QStringLiteral( "color_grayscale_average" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "color" ) ), fcnColorGrayscaleAverage, QStringLiteral( "Color" ) )
58855883
<< new QgsStaticExpressionFunction( QStringLiteral( "color_mix_rgb" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "color1" ) )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,11 @@ class TestQgsExpression: public QObject
12461246
QTest::newRow( "format_number large" ) << "format_number(9000000.0,0)" << false << QVariant( "9,000,000" );
12471247
QTest::newRow( "format_number many decimals" ) << "format_number(123.45600,4)" << false << QVariant( "123.4560" );
12481248
QTest::newRow( "format_number no decimals" ) << "format_number(1999.567,0)" << false << QVariant( "2,000" );
1249+
#if QT_VERSION >= QT_VERSION_CHECK( 5, 12, 0 )
1250+
QTest::newRow( "format_number language parameter" ) << "format_number(123457.00,2,'fr')" << false << QVariant( "123\u202F457,00" );
1251+
#else
1252+
QTest::newRow( "format_number language parameter" ) << "format_number(123457.00,2,'fr')" << false << QVariant( "123\u00A0457,00" );
1253+
#endif
12491254
QTest::newRow( "lower" ) << "lower('HeLLo')" << false << QVariant( "hello" );
12501255
QTest::newRow( "upper" ) << "upper('HeLLo')" << false << QVariant( "HELLO" );
12511256
QTest::newRow( "length" ) << "length('HeLLo')" << false << QVariant( 5 );

0 commit comments

Comments
 (0)
Please sign in to comment.