Skip to content

Commit cfdc8c2

Browse files
lbartolettinyalldawson
authored andcommittedJan 29, 2019
[BUGFIX] fix qgsRound for negative numbers. Fixes #20861
1 parent 774f025 commit cfdc8c2

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed
 

‎src/core/qgis.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,9 @@ inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
303303
*/
304304
inline double qgsRound( double number, double places )
305305
{
306+
double m = ( number < 0.0 ) ? -1.0 : 1.0;
306307
double scaleFactor = std::pow( 10.0, places );
307-
return std::trunc( number * scaleFactor + 0.5 ) / scaleFactor;
Code has comments. Press enter to view.
308+
return ( std::round( number * m * scaleFactor ) / scaleFactor ) * m;
308309
}
309310

310311

‎tests/src/core/testqgis.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ void TestQgis::testQgsAsConst()
342342

343343
void TestQgis::testQgsRound()
344344
{
345+
QGSCOMPARENEAR( qgsRound( 1234.567, 2 ), 1234.57, 0.01 );
346+
QGSCOMPARENEAR( qgsRound( -1234.567, 2 ), -1234.57, 0.01 );
345347
QGSCOMPARENEAR( qgsRound( 98765432198, 8 ), 98765432198, 1.0 );
346348
QGSCOMPARENEAR( qgsRound( 98765432198, 9 ), 98765432198, 1.0 );
347349
QGSCOMPARENEAR( qgsRound( 98765432198, 10 ), 98765432198, 1.0 );
@@ -357,9 +359,9 @@ void TestQgis::testQgsRound()
357359
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 5 ), 9.87654, 0.000001 );
358360
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 6 ), 9.876543, 0.0000001 );
359361
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 7 ), 9.8765432, 0.00000001 );
360-
QGSCOMPARENEAR( qgsRound( -9.8765432198765, 7 ), -9.876543, 0.000001 );
362+
QGSCOMPARENEAR( qgsRound( -9.8765432198765, 7 ), -9.8765432, 0.0000001 );
361363
QGSCOMPARENEAR( qgsRound( 9876543.2198765, 5 ), 9876543.219880, 0.000001 );
362-
QGSCOMPARENEAR( qgsRound( -9876543.2198765, 5 ), -9876543.219870, 0.000001 );
364+
QGSCOMPARENEAR( qgsRound( -9876543.2198765, 5 ), -9876543.219880, 0.000001 );
363365
QGSCOMPARENEAR( qgsRound( 9.87654321987654321, 13 ), 9.87654321987654, 0.0000000000001 );
364366
QGSCOMPARENEAR( qgsRound( 9.87654321987654321, 14 ), 9.876543219876543, 0.00000000000001 );
365367
QGSCOMPARENEAR( qgsRound( 9998.87654321987654321, 14 ), 9998.876543219876543, 0.00000000000001 );

0 commit comments

Comments
 (0)
Please sign in to comment.