Problem with Calculating a Total

My datatable has

Units - Decimal(18,1)
Price - Decimal(18,2)
Total - Decimal(18,2)

My Code

Dim Rate As Decimal
Dim Unit As Decimal
Dim Total As Decimal

Rate = 78.81
Unit = 5 * QTY / 18000
Total = Rate * Unit

When you do the math above for Unit you will get 2.77777777777 then Total would = 218.916666666, what I really want is for Unit to = 2.8 * Rate = 220.67. Units = 2.7 & Total = 218.91 is what is being stored in the database which is wrong since 2.7 * Rate = 220.67. Does anyone have an idea of how I can get what I need

Thanks,

Terry



Answer this question

Problem with Calculating a Total

  • JMHirst

    Your description does not seem to agree with the formula. In addition, You do not specify the value for QTY to test against. In general, I suspect you would benefit from turning Option Strict ON. You appear to have some implicit double to decimal conversion issues since both 78.81 and 8*QTY/18000 are evaluated as doubles. You can force rate to a decimal by changing the constant value to 78.81D. If you change the Unit formula to 5*QTY/18000D, you might get better results.

    Jim Wooley
    http://devauthority.com/blogs/jwooley



  • _Rab_

    Thank you so much that was exactly what I needed....

    Terry


  • Szilard

    Sorry QTY = 10000

    Rate is how much we charge per hour for this press, the 18000 is how many sheets the press can print an hour. So if I have 5 signatures(Units) * the QTY (10000) I need to print 50,000 sheets the press can print 18,000 an hour it will require 2.77777777777 hours to do the job, but I really just want it to be 2.8 hours since our billing is based on the tenth of an hour.

    I tried changing the rate to 78.10D did not make any difference. I hope this makes a little more since knowing what the QTY is.

    Thanks,

    Terry


  • shyam kodam

    You need to round the result of the Unit calculation as in the following:

    Unit = Math.Round(5 * QTY / 18000D, 1, MidpointRounding.AwayFromZero)

    Jim Wooley
    http://devauthority.com/blogs/jwooley



  • Problem with Calculating a Total