PMFBI
typical solution:
Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
I think your concern is misplaced...
?CInt(3.900)
4
?CInt(2.900)
3
BUT, when the fractional part is exactly 0.5,
CInt and CLng always round to the nearest *even* number.
?CInt(3.500)
4
?CInt(2.500)
2 <---- I don't think what you want!!!
The Fix and Int functions *truncate*
the fractional part of a number.
A typical solution is to truncate
(the division + 0.5):
mynum=3900
?Int(mynum/1000 + 0.5) * 1000
4000
mynum=2500
?Int(mynum/1000 + 0.5) * 1000
3000
mynum=3400
?Int(mynum/1000 + 0.5) * 1000
3000
=================
NEGATIVE NUMBERS
=================
But...will you have any negative numbers?
If so, in what direction do you want to round?
mynum = -3400
?Int(mynum/1000 + 0.5) * 1000
-3000
mynum = -3900
?Int(mynum/1000 + 0.5) * 1000
-4000
mynum = -2500
?Int(mynum/1000 + 0.5) * 1000
-2000
Maybe the result above is okay, but
if you wanted -2500 to round to -3000,
use the "sgn()/sgn()" trick:
mynum = -2500
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
-3000
mynum = -3400
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
-3000
mynum = -2600
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
-3000
plus, positive numbers work unaffected just like above:
mynum=3900
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
4000
mynum=2500
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
3000
mynum=3400
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
3000
============
RANGE
============
Plus, Int always returns a value of the same type as is passed in.
integer -- -32,768 to 32,767
long -- -2,147,483,648 to 2,147,483,647
mynum = 2147483647
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
2147484000
mynum = 2147483500
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
2147484000
mynum = 2147482500
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
2147483000
mynum = 2147483499
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
2147483000
mynum = -2147482500
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
-2147483000
mynum = -2147483499
?Sgn(mynum)*Int(Sgn(mynum)*mynum /1000 + 0.5) * 1000
-2147483000