473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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 6772
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 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


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
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
On Tue, 30 Sep 2003 08:57:51 GMT in comp.databases.ms-access, "Albert
D. Kallal" <ka****@msn.com> wrote:
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ciar?n | last post by:
I have a table with over a million rows and one of the fields contains amounts of money in text format. What is the most efficient way of converting this field to a number format that I can sum...
2
by: ehm | last post by:
I am working on creating an editable grid (for use in adding, deleting, and editing rows back to an Oracle database). I have a JSP that posts back to a servlet, which in turns posts to a WebLogic...
1
by: Barry | last post by:
Retoring Caret Position after text field correction -------------------------------------------------------------------------------- Hi, my code has the following form - function...
3
by: beta | last post by:
Hello everyone, I need some help here. If anyone has encountered this, knidly give me your advice. I have a command button (Command0) and a listbox (List1). Upon clicking the command button,...
0
by: Nithin | last post by:
My code as an txt attachment. I have 2 drop down list boxes that on selection populate text boxes from my database table. I am able to display the correct values in these text boxes. I have 2...
2
by: alexsg | last post by:
I'm setting up a resolutions database where each resolution will be copied from Word documents and pasted into a memo field. The resolution will be in the form: Resolution title <cr> Project no...
3
by: kvnsmnsn | last post by:
I've written the following Javascript file that includes an input text field and an output text field, the latter of which is initialized to zero. Each time the user enters a number in the input...
5
by: maury | last post by:
Hello, I have an SQL Server table with a (text) field in which there is a data value in this format: 200802290525 I need to build an update query the modifies this value subtracting 1 (one)...
1
by: cdonham | last post by:
When displaying a Memo Text field in a report using SQL Reporting Services the hidden Controls such as fonts margins, etc also show. When doing the same task in Crystal Reports, I convert the Memo...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.