Skip to content

Commit ebc7a35

Browse files
committedMay 12, 2013
Add floor and ceil functions
1 parent 2076031 commit ebc7a35

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed
 

‎resources/function_help/ceil-en_US

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h3>Function ceil()</h3>
2+
Rounds a number upwards.
3+
4+
<h4>Syntax</h4>
5+
<code>ceil(value)</code><br>
6+
7+
<h4>Arguments</h4>
8+
<code>value</code> - a number.
9+
<br>
10+
11+
<h4>Example</h4>
12+
<!-- Show example of function.-->
13+
<code>ceil(4.9) &rarr; 5</code><br>
14+
<code>ceil(-4.9) &rarr; -4</code><br>

‎resources/function_help/floor-en_US

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<h3>Function floor()</h3>
2+
Rounds a number downwards.
3+
4+
<h4>Syntax</h4>
5+
<code>floor(value)</code><br>
6+
7+
<h4>Arguments</h4>
8+
<code>value</code> - a number.
9+
<br>
10+
11+
<h4>Example</h4>
12+
<!-- Show example of function.-->
13+
<code>floor(4.9) &rarr; 4</code><br>
14+
<code>floor(-4.9) &rarr; -5</code><br>

‎src/core/qgsexpression.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ static QVariant fcnMin( const QVariantList& values, QgsFeature* , QgsExpression
468468
return QVariant( minVal );
469469
}
470470

471+
static QVariant fcnFloor( const QVariantList& values, QgsFeature* , QgsExpression* parent )
472+
{
473+
double x = getDoubleValue( values.at( 0 ), parent );
474+
return QVariant( floor( x ) );
475+
}
476+
477+
static QVariant fcnCeil( const QVariantList& values, QgsFeature* , QgsExpression* parent )
478+
{
479+
double x = getDoubleValue( values.at( 0 ), parent );
480+
return QVariant( ceil( x ) );
481+
}
482+
471483
static QVariant fcnToInt( const QVariantList& values, QgsFeature* , QgsExpression* parent )
472484
{
473485
return QVariant( getIntValue( values.at( 0 ), parent ) );
@@ -1291,6 +1303,7 @@ const QStringList &QgsExpression::BuiltinFunctions()
12911303
<< "asin" << "acos" << "atan" << "atan2"
12921304
<< "exp" << "ln" << "log10" << "log"
12931305
<< "round" << "rand" << "randf" << "max" << "min"
1306+
<< "floor" << "ceil"
12941307
<< "toint" << "toreal" << "tostring"
12951308
<< "todatetime" << "todate" << "totime" << "tointerval"
12961309
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
@@ -1334,6 +1347,8 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
13341347
<< new StaticFunction( "randf", 2, fcnRndF, QObject::tr( "Math" ) )
13351348
<< new StaticFunction( "max", -1, fcnMax, QObject::tr( "Math" ) )
13361349
<< new StaticFunction( "min", -1, fcnMin, QObject::tr( "Math" ) )
1350+
<< new StaticFunction( "floor", 1, fcnFloor, QObject::tr( "Math" ) )
1351+
<< new StaticFunction( "ceil", 1, fcnCeil, QObject::tr( "Math" ) )
13371352
<< new StaticFunction( "$pi", 0, fcnPi, QObject::tr( "Math" ) )
13381353
<< new StaticFunction( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
13391354
<< new StaticFunction( "toreal", 1, fcnToReal, QObject::tr( "Conversions" ) )

‎tests/src/core/testqgsexpression.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ class TestQgsExpression: public QObject
257257
QTest::newRow( "max(1,3.5,-2.1)" ) << "max(1,3.5,-2.1)" << false << QVariant( 3.5 );
258258
QTest::newRow( "min(-1.5)" ) << "min(-1.5)" << false << QVariant( -1.5 );
259259
QTest::newRow( "min(-16.6,3.5,-2.1)" ) << "min(-16.6,3.5,-2.1)" << false << QVariant( -16.6 );
260+
QTest::newRow( "floor(4.9)" ) << "floor(4.9)" << false << QVariant( 4. );
261+
QTest::newRow( "floor(-4.9)" ) << "floor(-4.9)" << false << QVariant( -5. );
262+
QTest::newRow( "ceil(4.9)" ) << "ceil(4.9)" << false << QVariant( 5. );
263+
QTest::newRow( "ceil(-4.9)" ) << "ceil(-4.9)" << false << QVariant( -4. );
260264

261265
// cast functions
262266
QTest::newRow( "double to int" ) << "toint(3.2)" << false << QVariant( 3 );

0 commit comments

Comments
 (0)
Please sign in to comment.