473,587 Members | 2,490 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 1437
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
1449
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 doesn't return any result like php exec function. I want to know about php equivalent exec function in Python. So If anyone know regarding this...
4
18879
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 no actual animations can be seen. Therefore I need a "delay" function. Is there a "delay" function in C++ that accepts a time argument in seconds or...
4
11306
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 Using this,I am first extracting data from given excel file into a temporary DataTable. After making some operations on that DataTable (like...
0
1163
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
4049
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). // Integer calculation (fast) int iround(const double x) { return (x>=0 ? static_cast<int>(x + 0.5) : static_cast<int>(x + 0.5) } // Floating...
42
3119
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 array (size is 10*10), setting all the values inside the 10*10 matrix to zero. My C-function looks like this:
7
1685
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
2828
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 apply. Can somebody tell me why? Thanks, Jess
2
1120
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
1953
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
7923
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5719
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2364
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 we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.