In Access VBA int(-0.005 * (10 ^ 6)) = -5001 Really -


consider this:

text access vba debug window:

? -0.005 * (10 ^ 6) -5000 ' expect  ? int(-5000) -5000 ' expect  ? int(-0.005 * (10 ^ 6)) -5001 ' not expected   ? int(-0.005 * 1000000) -5001 ' not expected  

here's function wrote expected round number number of decimal places shown.

am missing something. know ho might write function this?

    public function rounditup(avalue double, decimalplaces integer) variant         '         'rounding         '         'to round upwards towards next highest number, take advantage of way int() rounds negative numbers downwards, this:         '    - int( - [myfield])         '         'as shown above, int(-2.1) rounds down -3. therefore expression rounds 2.1 3.         '         'to round higher cent, multiply -100, round, , divide -100:         '    int(-100 * [myfield]) / -100          dim multiplier long          if decimalplaces <= 0             rounditup = null             exit function         end if          multiplier = 10 ^ decimalplaces          if avalue < 0              rounditup = int(avalue)          else              rounditup = -int(-avalue * multiplier) / multiplier          end if        end function 

and more debug window text showing how discovered error:

? rounditup(0.005,6) 0.005001   ? -avalue *  multiplier -5000   ? int(-avalue * multiplier) -5001   ? int(-5000) -5000   ? int((-avalue * multiplier)) -5001  

i think need fix function - issue you've got negative numbers idea of 'rounding up' changes.


Comments