424,687 Members | 2,065 Online
Need help? Post your question and get tips & solutions from a community of 424,687 IT Pros & Developers. It's quick & easy.

# VB rounding solved....problem converting back to text field

 P: n/a Hello Tom, Long time listener, first time caller... I have been working with vb code to automate a salary increase process that not only rounds to the nearest penny but also rounds to the nearest even penny using the code that follow. The problem I have is that once I set the even rounded value back to the text field it adds a 01 on the end of the raounded value (i.e. 1234.56 is 1234.5601) I am hoping that there are some textbox.value properties that can be modified to avoid this anomaly. FYI I have already set the textbox decimal places to 2 with no avail. Dim A As Single Dim B As Single Dim C As Integer Dim D As Single Dim NewSalary As String 'Calc percentage increase for new salary (Rounded to the nearest even penny) A = Me.txtCurrSalary.Value * ((Me.txtActPercent.Value / 100) + 1) B = Int((A * 100) + 0.5) / 100 C = Int((B * 100) / 2) - ((B * 100) / 2) D = (B - (C / 100)) Me.txtNewFinalSalary.Value = D The following remarked lines included to display debug info. '(i.e. me.txtCurrSalary.Value = 3220.52 & me.txtActPercent.value = 5) 'A = 3381.546 'B = 3381.55 'C = -1 'D = 3381.56 'me.txtNewFinalSalary.Value = 3381.5601 <:::::problem Thanks for any assistance in advanced. -Augustus ....Take me out with a screaming O tom! Nov 12 '05 #1
3 Replies

 P: n/a On 29 Sep 2003 12:27:51 -0700 in comp.databases.ms-access, ma*******@yahoo.com (Caesar Augustus) wrote: Hello Tom,Long time listener, first time caller...I have been working with vb code to automate a salary increase processthat not only rounds to the nearest penny but also rounds to thenearest even penny using the code that follow. The problem I have isthat once I set the even rounded value back to the text field it addsa 01 on the end of the raounded value (i.e. 1234.56 is 1234.5601)I am hoping that there are some textbox.value properties that can bemodified to avoid this anomaly. FYI I have already set the textboxdecimal places to 2 with no avail.Dim A As SingleDim B As SingleDim C As IntegerDim D As SingleDim NewSalary As String'Calc percentage increase for new salary (Rounded to the nearest evenpenny)A = Me.txtCurrSalary.Value * ((Me.txtActPercent.Value / 100) + 1)B = Int((A * 100) + 0.5) / 100C = Int((B * 100) / 2) - ((B * 100) / 2)D = (B - (C / 100))Me.txtNewFinalSalary.Value = DThe following remarked lines included to display debug info.'(i.e. me.txtCurrSalary.Value = 3220.52 & me.txtActPercent.value = 5)'A = 3381.546'B = 3381.55'C = -1'D = 3381.56'me.txtNewFinalSalary.Value = 3381.5601 <:::::problem Seems to work for me, is txtNewFinalSalary bound to anything? If so what type? -- A)bort, R)etry, I)nfluence with large hammer. Nov 12 '05 #2

 P: n/a I am going to strongly suggest you do NOT use real numbers for financial calculations. I have written a good many payroll systems, and I can assure you that is not even close to even being possible to consider writing any computer application where you use single, or double value numbers for financial calculations. Computers can only approximate represent real values. As a result, all kinds of round problems will occur. Try the following code in a module, and not the output: Dim s As Single Dim i As Integer For i = 1 To 10 s = s + 0.01 Next i Debug.Print s Debug.Print s = 0.1 The above actaully results in: 9.999999E-02 False Wow, look at the above. We have only added a number 10 times, and look at the huge rounding errors you got. Note how the comparison of "s = .1" results in return a false value. Further, note how we do not get a expected answer of .1 The reason for the above is that computers use "0" and "1" to represent a real number. In the above case, the computer has to try and represent 1/100th. You can't do that with a real number in binary. The solution is to NOT use real numbers in your calculations. You should use integers, as they do NOT suffer this routing problem. As it turns out, we have a scaled integer called "currency". It is allows up to 4 decimal places. However, it is really a fake real number, and in fact is stored as a integer with a "scale" of 4 decimal places. So, lets try the above code with a currency type: Dim s As Currency Dim i As Integer For i = 1 To 10 s = s + 0.01 Next i Debug.Print s Debug.Print s = 0.1 0.1 True As you can see, there is no rounding problems. This is also how desktop calculators avoid the rounding problem also. However, in the computing world, the above is a daily problem we have had to deal with for the last 50 years (This is also one of the first things you are taught in computing science). That is why I suggest you use currency type. -- Albert D. Kallal (MVP) Edmonton, Alberta Canada ka****@msn.com http://www.attcanada.net/~kallal.msn Nov 12 '05 #3

 P: n/a On Tue, 30 Sep 2003 08:57:51 GMT in comp.databases.ms-access, "Albert D. Kallal" wrote: I am going to strongly suggest you do NOT use real numbers for financialcalculations. I have written a good many payroll systems, and I can assureyou that is not even close to even being possible to consider writing anycomputer application where you use single, or double value numbers forfinancial calculations. Computers can only approximate represent realvalues. As a result, all kinds of round problems will occur.Try the following code in a module, and not the output: Dim s As Single Dim i As Integer For i = 1 To 10 s = s + 0.01 Next i Debug.Print s Debug.Print s = 0.1The above actaully results in: 9.999999E-02False You didn't need all that, just a simple: ?3.11 - 3.1 will show up the floating point error. -- A)bort, R)etry, I)nfluence with large hammer. Nov 12 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion.