Solved

VB Script Control is not processing rounding correctly.

Posted on 2013-11-25
8
226 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:AmitJain001
ID: 39673978
because the Round Function does not return 80.5 to 81. hence I tried formatting function.
0
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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.
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…
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…

776 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