Hi Jamal,
your RoundTest() crashes if iDec is negative, while a X# round like Round ( 9367, -2 ) shows correctly 9400.
Here´s the link to the Round() source code:
https://github.com/X-Sharp/XSharpPublic ... h.prg#L246
I think the problem is the part where iDec > 0 is handled.
Code: Select all
...
IF iDec > 0
// Round after decimal point
IF iDec > MAX_DECIMALS
iDec := MAX_DECIMALS
ENDIF
r8 := Math.Round( r8, iDec, MidpointRounding.AwayFromZero )
ELSE
...
In the earlier days VO had round problems too. I can't remember who posted a fix back then, but i modified it a little bit, so it looks now:
!! This is a quick shot only !!
Code: Select all
FUNCTION Round2 ( fVal AS USUAL , iDec AS INT ) AS USUAL PASCAL
IF iDec > 0
IF IsFloat ( fVal ) .or. isdecimal ( fVal )
IF fVal < 0
fVal := fVal - ( 1 / 10 ^ ( iDec + 1 ) )
ELSE
fVal := fVal + ( 1 / 10 ^ ( iDec + 1 ) )
ENDIF
ENDIF
ENDIF
RETURN XSharp.RT.Functions.Round (fVal , iDec )
Here are some Round() and Round2() results
Code: Select all
? "Round()"
? "-------"
?
? Round ( 65.475 , 2 ) // must be 65.48 instead of 65.47
? Round ( -8.075 , 2 ) // must be -8.08 instead of -8.07
? Round ( 8.075 , 2 ) // must be 8.08 instead of 8.07
? Round ( 294.405 , 2 ) // must be 294.41 instead of 294.40
? Round (4.475,2) // must be 4.48 instead of 4.47
? Round ( 65.475 , -2 ) // 100
? Round ( 9367, -2 ) // 9400
? Round (10.4, 0) // 10
? Round (10.5, 0) // 11
? Round (10.51, 0) // 11
? Round (10.49999999999999, 2) // 10.50
? Round (101.99, -1) // 100
? Round (109.99, -1) // 110
? Round (109.99, -2) // 100
? Round2 (-101.99, -1) // -100
? "Round2()"
? "-------"
?
? Round2 ( 65.475 , 2 ) // 65.48
? Round2 ( -8.075 , 2 ) // -8.08
? Round2 ( 8.075 , 2 ) // 8.08
? Round2 ( 294.405 , 2 ) // 294.41
? Round2 (4.475,2) // 4.48
? Round2 ( 65.475 , -2 ) // 100
? Round2 ( 9367, -2 ) // 9400
? Round2 (10.4, 0) // 10
? Round2 (10.5, 0) // 11
? Round2 (10.51, 0) // 11
? Round2 (10.49999999999999, 2) // 10.50
? Round2 (101.99, -1) // 100
? Round2 (109.99, -1) // 110
? Round2 (109.99, -2) // 100
? Round2 (-101.99, -1) // -100
regards
Karl-Heinz