Microsoft Knowledge Base Email Alertz

KBAlertz.com: (165373) - When you run a Visual Basic for Applications macro that performs numeric calculations that result in a decimal value, the result that is returned may be incorrect. For example, for the following calculation n = 301.84 - 301 Visual Basic for...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 165373 - Last Review: January 19, 2007 - Revision: 2.3

OFF97: Rounding Errors in Visual Basic For Applications

This article was previously published under Q165373

SYMPTOMS

When you run a Visual Basic for Applications macro that performs numeric calculations that result in a decimal value, the result that is returned may be incorrect. For example, for the following calculation
n = 301.84 - 301
Visual Basic for Applications returns 0.839999999999975. The correct result is 0.84.

WORKAROUND

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

To return the correct result rounded to a specific decimal place, use the Format function.

The following example macro returns the correct result (.84) for the calculation in this article:
   n = CDbl(Format(301.84 - 301, "0.00"))
				
For more information about using the Format function, click the Office Assistant in the Visual Basic Editor, type Format Function, click Search, and then click to view the "Format Function" topic.

NOTE: If the Assistant is hidden, click the Office Assistant button on the Standard toolbar. If Visual Basic for Applications Help is not installed on your computer, please see the following article in the Microsoft Knowledge Base:
120802  (http://kbalertz.com/Feedback.aspx?kbNumber=120802/EN-US/ ) Office: How to Add/Remove a Single Office Program or Component

STATUS

Microsoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

For additional information, please see the following articles in the Microsoft Knowledge Base:
126455  (http://kbalertz.com/Feedback.aspx?kbNumber=126455/EN-US/ ) How to Avoid Rounding & Overflow Probs on Pentium Processors
42980  (http://kbalertz.com/Feedback.aspx?kbNumber=42980/EN-US/ ) (Complete) Tutorial to Understand IEEE Floating-Point Errors
69333  (http://kbalertz.com/Feedback.aspx?kbNumber=69333/EN-US/ ) How to Work Around Floating-Point Accuracy/Comparison Problems

APPLIES TO
  • Microsoft Word 97 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Office 97 Standard Edition
Keywords: 
kbprogramming KB165373
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Dave Edwards Report As Irrelevant  
Written: 11/16/2004 8:46 AM
If you try and use the additional Microsoft-provided rounding functions (http://support.microsoft.com/kb/196652/EN-US/), because there aren't any in VBA, you may find like I did that the result is not always as expected. Use Format before rounding is the advice.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please