Solved

VB Script Control is not processing rounding correctly.

Posted on 2013-11-25
8
225 Views
Last Modified: 2013-12-10
Hi Experts,

In vb script I have sent an arithmetic expression which is divisible by 2; is not rounding correctly : eg;
"ScriptControl1.Eval" functions returns 80 for expression 80.5 but it should be rounded to next number i.e. 81, but when you pass any odd number it will return correct value.
how can this be achieved

I have tried evaluating following expression but it also throws error :

Val(Format((( 23 + 24 + 22 + 18 + 21 + 23 + 21 + 23 ) /2.0 ) ,"0") )

Thanks.
0
Comment
Question by:AmitJain001
  • 3
  • 3
  • 2
8 Comments
 
LVL 13

Expert Comment

by:akb
ID: 39673907
I've had similar problems with rounding. Try adding something like 0.0000000001 to the number before rounding it. If, for example, your numbers are 4 decimal places you can safely add 0.00001 to them before rounding. this should guarantee an accurate result.
0
 

Author Comment

by:AmitJain001
ID: 39673919
Actually the string ('( 23 + 24 + 22 + 18 + 21 + 23 + 21 + 23 ) /2.0 )') is being generated at run time where I cannot confirm where to add that number. because there might be more that one places where it may require to be added.
0
 
LVL 7

Expert Comment

by:Ironhoofs
ID: 39673960
I suggest you use the Round() function, because formatting or rounding the result to zero decimal places behave differently. Example:
Round((23 + 24 + 22 + 18 + 21 + 23 + 21 + 23 ) / 2.0)

Open in new window

See W3School VBScript Round Function for more information.
0
 

Author Comment

by:AmitJain001
ID: 39673978
because the Round Function does not return 80.5 to 81. hence I tried formatting function.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Accepted Solution

by:
Ironhoofs earned 500 total points
ID: 39673994
Thats because vbscript uses "Bankers rounding" (See Rounding). Simply adding a small amount would not do the trick, you would have to write your own rounding function that takes into account if the basenumber is odd or even.
0
 
LVL 13

Expert Comment

by:akb
ID: 39674001
It does work actually
0
 

Author Comment

by:AmitJain001
ID: 39674198
hi Ironhoofs, I have an arithmetic string generated by my application at run time where I am not aware at which place and a particular addition or subtraction or division is taken place or the round function will be required. because the rounding also specified by application user it self.

user may use : Round( ((Round((a+B)/2,0) + C) *3),2) or what ever required by him, so its becoming difficult to evaluate manually by re-writing own function that's why I used vbscript in my code.
0
 
LVL 7

Expert Comment

by:Ironhoofs
ID: 39674283
I fully understand that youprefer to use vbscript as a "out of the box" function parser. I wasn't suggesting writing your own parser, just a replacement for the Round() function.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now