473,511 Members | 16,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with using Round function on even numbers

round function does not work properly with even number having decimal place .5. eg: Round(2.5)is 2 instead of 3.
Oct 28 '10 #1
1 1432
code green
1,726 Recognized Expert Top Contributor
Most languages seem to use bankers' rounding.
This means midway values are rounded to the nearest even value.
Expand|Select|Wrap|Line Numbers
  1. So round(2.5) becomes 2.
  2. round(3.55,1) becomes 3.6
To force a mid value always up, the trick is to add a constant EPSILON.
This is an insignificant small value that won't throw out the sums but enough to trip rounding the right way.
Expand|Select|Wrap|Line Numbers
  1. round((qty * price) + EPSILON,2)
The same technique is used when comparing floating point numbers
2.5 == 5/2 may not return true, so instead compare the result to EPSILON.
Expand|Select|Wrap|Line Numbers
  1. if((2.5 - 5/2)) < EPSILON)
So EPSILON is your tolerance. It should be smaller than the precision you will be displaying but slightly larger than the precision you are calculating.
So if calculating in three decimal places, but displayng the result in two, EPSILON would be 0.001.
Oct 28 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1447
by: Mosas | last post by:
Dear All, Php has exec function to run system commands and this function return last line of system command result. But in Python I can see only os.system() function to run system command but it...
4
18865
by: Shuo Xiang | last post by:
Greetings: I'm working on some graphical game and need to animate certain things (by continuously changing the x/y coordinate and update the display), however, the system is blinkingly fast so...
4
11302
by: RK | last post by:
Hi, In my application, I need to copy data from an Excel file into a SQL table. The article related to this can be found at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B306572 ...
0
1156
by: yulyos | last post by:
Hi, Round Number Function You can download a small example: Watch and copy examples of Source Code in VB.NET yulyos4vbnet@yahoo.com http://www.geocities.com/yulyos4vbnet Have a nice day
6
4045
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
42
3084
by: Martin Jørgensen | last post by:
Hi, I'm trying to move a matlab program into c language. For those who knows matlab, this is the line I want to program in c: hx(1:nx,1:ny) = 0; % nx=10, ny=10 It works on a 2-dimensional...
7
1679
by: lintolawrance | last post by:
hello, i am a student trying to learn php.i have a doubt about a sentence written in the textbook.the sentence is "upload the file to your web server", for that what i have to done.
28
2817
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
2
1117
by: tim007 | last post by:
Hi all, I have a doubt regarding the "Using" statement if i write a code like this.... using (sqlDataReader) { do something ....
5
1945
by: vsts2007 | last post by:
Hi all, i need help regarding one worksheet function: can we use ROUND worksheet function in access also, i need suggestions (how to use) plz help me
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.