Skip to content

Commit 3f0171b

Browse files
lbartolettinyalldawson
authored andcommittedFeb 1, 2019
[BUGFIX] fix qgsRound for negative numbers. Fixes #20861
(cherry picked from commit cfdc8c2)
1 parent 3462511 commit 3f0171b

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
@@ -316,8 +316,9 @@ inline bool qgsDoubleNearSig( double a, double b, int significantDigits = 10 )
316316
*/
317317
inline double qgsRound( double number, double places )
318318
{
319+
double m = ( number < 0.0 ) ? -1.0 : 1.0;
319320
double scaleFactor = std::pow( 10.0, places );
320-
return std::trunc( number * scaleFactor + 0.5 ) / scaleFactor;
321+
return ( std::round( number * m * scaleFactor ) / scaleFactor ) * m;
321322
}
322323

323324

‎tests/src/core/testqgis.cpp

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

340340
void TestQgis::testQgsRound()
341341
{
342+
QGSCOMPARENEAR( qgsRound( 1234.567, 2 ), 1234.57, 0.01 );
343+
QGSCOMPARENEAR( qgsRound( -1234.567, 2 ), -1234.57, 0.01 );
342344
QGSCOMPARENEAR( qgsRound( 98765432198, 8 ), 98765432198, 1.0 );
343345
QGSCOMPARENEAR( qgsRound( 98765432198, 9 ), 98765432198, 1.0 );
344346
QGSCOMPARENEAR( qgsRound( 98765432198, 10 ), 98765432198, 1.0 );
@@ -354,9 +356,9 @@ void TestQgis::testQgsRound()
354356
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 5 ), 9.87654, 0.000001 );
355357
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 6 ), 9.876543, 0.0000001 );
356358
QGSCOMPARENEAR( qgsRound( 9.8765432198765, 7 ), 9.8765432, 0.00000001 );
357-
QGSCOMPARENEAR( qgsRound( -9.8765432198765, 7 ), -9.876543, 0.000001 );
359+
QGSCOMPARENEAR( qgsRound( -9.8765432198765, 7 ), -9.8765432, 0.0000001 );
358360
QGSCOMPARENEAR( qgsRound( 9876543.2198765, 5 ), 9876543.219880, 0.000001 );
359-
QGSCOMPARENEAR( qgsRound( -9876543.2198765, 5 ), -9876543.219870, 0.000001 );
361+
QGSCOMPARENEAR( qgsRound( -9876543.2198765, 5 ), -9876543.219880, 0.000001 );
360362
QGSCOMPARENEAR( qgsRound( 9.87654321987654321, 13 ), 9.87654321987654, 0.0000000000001 );
361363
QGSCOMPARENEAR( qgsRound( 9.87654321987654321, 14 ), 9.876543219876543, 0.00000000000001 );
362364
QGSCOMPARENEAR( qgsRound( 9998.87654321987654321, 14 ), 9998.876543219876543, 0.00000000000001 );

0 commit comments

Comments
 (0)
Please sign in to comment.