Solved

VB Script Control is not processing rounding correctly.

Posted on 2013-11-25
8
232 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON Response and request in VB6 application 11 824
Saving history changes to sub form 4 52
VBA filters 2 71
Excel Automation VBA 19 104
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

730 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