mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-02 00:02:12 -05:00
Add round function to QgsExpression
This commit is contained in:
parent
b72ddcab7e
commit
4f58f13822
@ -29,6 +29,13 @@
|
||||
#include "qgsgeometry.h"
|
||||
#include "qgslogger.h"
|
||||
|
||||
//non qt includes
|
||||
#include <cmath>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define round(x) ((x) >= 0 ? floor((x)+0.5) : floor((x)-0.5))
|
||||
#endif
|
||||
|
||||
// from parser
|
||||
extern QgsExpression::Node* parseExpression( const QString& str, QString& parserErrorMsg );
|
||||
|
||||
@ -768,6 +775,13 @@ static QVariant fcnGeomPerimeter( const QVariantList& , QgsFeature* f, QgsExpres
|
||||
return QVariant( calc->measurePerimeter( f->geometry() ) );
|
||||
}
|
||||
|
||||
static QVariant fcnRound( const QVariantList& values , QgsFeature* f, QgsExpression* parent )
|
||||
{
|
||||
double number = getDoubleValue( values.at( 0 ), parent );
|
||||
double scaler = pow( 10.0, getIntValue( values.at( 1 ), parent ) );
|
||||
return QVariant( round( number * scaler ) / scaler );
|
||||
}
|
||||
|
||||
QList<QgsExpression::FunctionDef> QgsExpression::gmBuiltinFunctions;
|
||||
|
||||
const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
|
||||
@ -788,6 +802,7 @@ const QList<QgsExpression::FunctionDef> &QgsExpression::BuiltinFunctions()
|
||||
<< FunctionDef( "ln", 1, fcnLn, QObject::tr( "Math" ) )
|
||||
<< FunctionDef( "log10", 1, fcnLog10, QObject::tr( "Math" ) )
|
||||
<< FunctionDef( "log", 2, fcnLog, QObject::tr( "Math" ) )
|
||||
<< FunctionDef( "round", 2, fcnRound, QObject::tr( "Math" ) )
|
||||
// casts
|
||||
<< FunctionDef( "toint", 1, fcnToInt, QObject::tr( "Conversions" ) )
|
||||
<< FunctionDef( "toreal", 1, fcnToReal, QObject::tr( "Conversions" ) )
|
||||
|
@ -246,6 +246,8 @@ class TestQgsExpression: public QObject
|
||||
QTest::newRow( "log10(100)" ) << "log10(100)" << false << QVariant( 2. );
|
||||
QTest::newRow( "log(2,32)" ) << "log(2,32)" << false << QVariant( 5. );
|
||||
QTest::newRow( "log(10,1000)" ) << "log(10,1000)" << false << QVariant( 3. );
|
||||
QTest::newRow( "round(1234.557,2) - round up" ) << "round(1234.557,2)" << false << QVariant( 1234.56 );
|
||||
QTest::newRow( "round(1234.554,2) - round down" ) << "round(1234.554,2)" << false << QVariant( 1234.55 );
|
||||
|
||||
// cast functions
|
||||
QTest::newRow( "double to int" ) << "toint(3.2)" << false << QVariant( 3 );
|
||||
|
Loading…
x
Reference in New Issue
Block a user