WinXP and VB 6 Format$

Posted on 2002-04-16
Last Modified: 2010-05-02
We are having trouble with the format$ command under Windows XP.

It has to do with rounding of numbers to 2 decimal places (eg Format$(ANumber, "0.00").  
If you pass in a number such as 1.23, it works fine.
If you pass in 1.265, the result is 1.27 - this is ok.
If you pass in 1.257, the result is 1.26 - this is ok.
If you pass in 1.253, the result is 1.25 - this is ok.

If you pass in 1.235, the result is 1.23 - incorrect!
If you pass in 1.236, the result is 1.23 - incorrect!
If you pass in 1.239, the result is 1.23 - incorrect!

It appears to look at the second decimal place when evaluating the 3rd and subsequent places. If the 2nd number is < 5, then it ignores any possible rounding on the 3rd digit.

We have tried to "over ride" the format function by declaring our own, and if it meets any other criteria other than "0.00", we pass it out to VBA.Format$, otherwise we manipulate the code to round the number accordingly, but we think that this is a dangerous practice, and we are reluctant to release software with this in it.

Has anyone seen this, and if so, do they have an alternate solution?
Question by:finster

Expert Comment

ID: 6946409
Have you tried using the Round function?
Round(expression [,numdecimalplaces])

LVL 39

Expert Comment

ID: 6946415
I think there are several thinks at hand here that may cause (or help cause) this problem. Even though I could not find any specific information on this particular problem, I may have some thinks here that can be of help.

First of all, try a possible solution. If you are handy with API functions in VB, look up the documentation of GetNumberFormat. You can use it to do exactly what VB does. I think VB internally uses the same function, but if it doesn't, or if there's something else that goes wrong, you at least got that sorted out by using the API approach. And if it indeed does work correctly, you'll have a workaround as well.

Meanwhile I'll try to similate your situation, to see if I have the same problem on win2k (I'm sorry, I do not have winXP installed on any machine).

(I find the name misleading, it does not get the details of the format, you may give it. The function returns a formatted string, just as VB's Format$)

LVL 39

Expert Comment

ID: 6946454
Hmm, I do not have the same problem on win2k, unfortunately, it appears to work correctly.

> I think there are several thinks at hand
I meant things, of course ;-)

Let's give that some thought. Basically, what happens here is that Format$, FormatNumber and GetNumberFormat all call into the same API. I do not know if Format$ and FormatNumber do any pre-formatting before passing the data to GetNumberFormat, but what I do know is that GetNumberFormat is part of the Internationalization API in Windows. You probably know the settings form in Control Panel, giving you all kind of options about numbers and all. I have never seen an option about rounding, but maybe something like that is added in XP? You may also want to try several other kinds of settings here, like increasing the number of digits in both the Currency and Number tabs or changing the locale country. To see of the settings are actually in effect in your VB app, simply change the Decimal Symbol on the Numbers tab to some strange symbol. You should see the change immediate in the Format$-output.

If all this doesn't help and the GetNumberFormat is to no avail either, I'm afraid you'll be stuck with using combinations of Fix, Round and the like.

Important note: Round() does not operate correctly on values like 1.255. It converts this value (incorrectly) to 1.25 instead of the correct 1.26.

LVL 43

Accepted Solution

TimCottee earned 75 total points
ID: 6947168
finster, the problem is due to the way "rounding" is implemented in VB. The following articles should help in understanding this:


LVL 15

Expert Comment

ID: 6947299
>If you pass in 1.236, the result is 1.23 - incorrect!
>If you pass in 1.239, the result is 1.23 - incorrect!

Are you sure?  There is a doubt and inconsistency if last digit is 5, but not for these:
Try in debug window:
? Format$(1.236, "0.00")
? Format$(1.239, "0.00")

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question