Solved

VB Script Control is not processing rounding correctly.

Posted on 2013-11-25
8
223 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
because the Round Function does not return 80.5 to 81. hence I tried formatting function.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Accepted Solution

by:
Ironhoofs earned 500 total points
Comment Utility
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
Comment Utility
It does work actually
0
 

Author Comment

by:AmitJain001
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
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…

743 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

14 Experts available now in Live!

Get 1:1 Help Now