Skip to content
Permalink
Browse files

Add floor and ceil functions

  • Loading branch information
nyalldawson committed May 12, 2013
1 parent 2076031 commit ebc7a35f7bb68eca5fef37ad349e5f6d8bf12e0c
@@ -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>
@@ -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>
@@ -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 ) );
@@ -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"
@@ -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" ) )
@@ -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 );

0 comments on commit ebc7a35

Please sign in to comment.
You can’t perform that action at this time.