In a table, the variable RetailPrice is a single value - displayed on a form say value £1.67 It has to be discounted and the result needs to be rounded to 2 sig figs before any other calulation is applied to it. DiscPerc is a single variable value of 50.00. Qty is a single variable value of 44. This gives the DiscAmt and the LineNet. This is in an button click event subroutine:-
Dim Qty, DiscPerc, DiscAmt, LineNet As Single
Dim DoubleRetailPrice As Double
Qty = Me.txtQty
DiscPerc = Me.txtDiscount
DoubleRetailPrice = RetailPrice + 0.000001
DiscAmt = Qty * Round((RetailPrice * (DiscPerc / 100)), 2)
LineNet = Qty * Round((RetailPrice * ((100 - DiscPerc) / 100)), 2)
LineNet = Round(LineNet, 2)
This should give a value of 0.84 * 44 = 36.96
However its gives a value of 0.83 * 44 = 36.52
By adding a line above and Defining DoubleRetailPrice as a double - as below:-
DiscAmt = Qty * Round((DoubleRetailPrice * (DiscPerc / 100)), 2)
LineNet = Qty * Round((DoubleRetailPrice * ((100 - DiscPerc) / 100)), 2)
LineNet = Round(LineNet, 2)
DoubleRetailPrice = 1.66999995708466 which when multiplied by 44 gives 36.52 - again wrong
However if I add this code instead
DoubleRetailPrice = RetailPrice + 0.000001 ( I get a value of 1.670001 which actually gives the correct calulation if you replace the variable RetailPrice with DoubleRetailPrice)
In the immediate section in VB window ;-
? Round(0.12335,4) = 0.1234 ( what you would expect, so rounding is correct )
? 1.67 = RetailPrice gives True
? Round( ( RetailPrice * ( DiscPerc / 100 ) ) ,2) gives 0.83
? Round( ( 1.67 * ( DiscPerc / 100 ) ) ,2) gives 0.84
Surely there must be an error in VB6?
I have created a database to show this if anyone wants it.testdb.accdb