Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Prevention of Rounding 1

Status
Not open for further replies.

CGill

Programmer
Jan 2, 2003
51
GB
Using the below piece of code I need to return an answer of 31.45 which is the literal translation of my calculation. Due to MS Access rounding this answer is intepreted as 32.

Me.Hours_Week = 37
Me.TotalTaken2 = 39

--------------------------------------------------------
Const Year As Integer = 52
Dim Calc As Integer
Dim Hrs As Integer

Hrs = Me.Hours_Week
Calc = (Year - (Me.TotalTaken2 \ 5))

Me.ContractualHoursOfEmployment = (Hrs * Calc) \ 52
--------------------------------------------------------
 
Two problems:

1. You're defining Calc as an integer - by definition, this is a whole number
2. In your equation you're using integer division ( \ ) rather than ordinary division ( / )

Try something like this (untested) mod of your code instead:
Code:
Me.Hours_Week  = 37
Me.TotalTaken2 = 39

--------------------------------------------------------
Const Year As Integer = 52
Dim Calc As Double
Dim Hrs As Integer

Hrs = Me.Hours_Week
Calc = (Year - (Me.TotalTaken2 / 5))

Me.ContractualHoursOfEmployment = (Hrs * Calc) / 52
Hope this helps.

[pc2]
 
Thanks for the reply I hadn't notice the division method have changed that now, the reason for declaring Calc as integer is that it is a variable number from 16 - 37. How should I declare the value ?
 
I declared it as Double in my reworking of your code, but you can experiment with other data types.

Here are some examples of different numeric data types:

Byte
Stores numbers from 0 to 255 (no fractions).

Decimal
Stores numbers from –10^38–1 through 10^38–1 (.adp)
Stores numbers from –10^28–1 through 10^28–1 (.mdb)

Integer
Stores numbers from –32,768 to 32,767 (no fractions).

Long
Stores numbers from –2,147,483,648 to 2,147,483,647 (no fractions).

Single
Stores numbers from –3.402823E38 to –1.401298E–45 for negative values and from 1.401298E–45 to 3.402823E38 for positive values.

Double
Stores numbers from –1.79769313486231E308 to
–4.94065645841247E–324 for negative values and from
4.94065645841247E–324 to 1.79769313486231E308 for positive values.

Hope this is helpful.

[pc2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top