?
Solved

VB Script Control is not processing rounding correctly.

Posted on 2013-11-25
8
Medium Priority
?
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 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