Skip to content

Commit

Permalink
Add floor and ceil functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 12, 2013
1 parent 2076031 commit ebc7a35
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
14 changes: 14 additions & 0 deletions resources/function_help/ceil-en_US
@@ -0,0 +1,14 @@
<h3>Function ceil()</h3>
Rounds a number upwards.

<h4>Syntax</h4>
<code>ceil(value)</code><br>

<h4>Arguments</h4>
<code>value</code> - a number.
<br>

<h4>Example</h4>
<!-- Show example of function.-->
<code>ceil(4.9) &rarr; 5</code><br>
<code>ceil(-4.9) &rarr; -4</code><br>
14 changes: 14 additions & 0 deletions resources/function_help/floor-en_US
@@ -0,0 +1,14 @@
<h3>Function floor()</h3>
Rounds a number downwards.

<h4>Syntax</h4>
<code>floor(value)</code><br>

<h4>Arguments</h4>
<code>value</code> - a number.
<br>

<h4>Example</h4>
<!-- Show example of function.-->
<code>floor(4.9) &rarr; 4</code><br>
<code>floor(-4.9) &rarr; -5</code><br>
15 changes: 15 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -468,6 +468,18 @@ static QVariant fcnMin( const QVariantList& values, QgsFeature* , QgsExpression
return QVariant( minVal );
}

static QVariant fcnFloor( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
double x = getDoubleValue( values.at( 0 ), parent );
return QVariant( floor( x ) );
}

static QVariant fcnCeil( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
double x = getDoubleValue( values.at( 0 ), parent );
return QVariant( ceil( x ) );
}

static QVariant fcnToInt( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
return QVariant( getIntValue( values.at( 0 ), parent ) );
Expand Down Expand Up @@ -1291,6 +1303,7 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "asin" << "acos" << "atan" << "atan2"
<< "exp" << "ln" << "log10" << "log"
<< "round" << "rand" << "randf" << "max" << "min"
<< "floor" << "ceil"
<< "toint" << "toreal" << "tostring"
<< "todatetime" << "todate" << "totime" << "tointerval"
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
Expand Down Expand Up @@ -1334,6 +1347,8 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "randf", 2, fcnRndF, QObject::tr( "Math" ) )
<< new StaticFunction( "max", -1, fcnMax, QObject::tr( "Math" ) )
<< new StaticFunction( "min", -1, fcnMin, QObject::tr( "Math" ) )
<< new StaticFunction( "floor", 1, fcnFloor, QObject::tr( "Math" ) )
<< new StaticFunction( "ceil", 1, fcnCeil, QObject::tr( "Math" ) )
<< new StaticFunction( "$pi", 0, fcnPi, QObject::tr( "Math" ) )
<< new StaticFunction( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
<< new StaticFunction( "toreal", 1, fcnToReal, QObject::tr( "Conversions" ) )
Expand Down
4 changes: 4 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -257,6 +257,10 @@ class TestQgsExpression: public QObject
QTest::newRow( "max(1,3.5,-2.1)" ) << "max(1,3.5,-2.1)" << false << QVariant( 3.5 );
QTest::newRow( "min(-1.5)" ) << "min(-1.5)" << false << QVariant( -1.5 );
QTest::newRow( "min(-16.6,3.5,-2.1)" ) << "min(-16.6,3.5,-2.1)" << false << QVariant( -16.6 );
QTest::newRow( "floor(4.9)" ) << "floor(4.9)" << false << QVariant( 4. );
QTest::newRow( "floor(-4.9)" ) << "floor(-4.9)" << false << QVariant( -5. );
QTest::newRow( "ceil(4.9)" ) << "ceil(4.9)" << false << QVariant( 5. );
QTest::newRow( "ceil(-4.9)" ) << "ceil(-4.9)" << false << QVariant( -4. );

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

0 comments on commit ebc7a35

Please sign in to comment.