473,763 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rounding routine

By any chance, anyone got a rounding routine that does a work around the VB
"round" bug?
I still find it amazing that a company as large as Microsoft would put out a
math package that is so problematic.
Jul 17 '05 #1
20 11728
> By any chance, anyone got a rounding routine that does a work around
the VB
"round" bug?
I still find it amazing that a company as large as Microsoft would put out a math package that is so problematic.


It's not a bug, just (in my opinion) a dumb decision on their part.
Anyway, use the Format function (without a format specifier) as it uses
"normal" rounding rather than the Banker's Rounding (which you are
referring to as a "bug"). For example,

Debug.Print Format(2.5)

Rick - MVP

Jul 17 '05 #2
On Sat, 26 Jun 2004 04:15:15 -0400, "Rick Rothstein"
<ri************ @NOSPAMcomcast. net> wrote:
By any chance, anyone got a rounding routine that does a work around

the VB
"round" bug?
I still find it amazing that a company as large as Microsoft would put

out a
math package that is so problematic.


It's not a bug, just (in my opinion) a dumb decision on their part.
Anyway, use the Format function (without a format specifier) as it uses
"normal" rounding rather than the Banker's Rounding (which you are
referring to as a "bug"). For example,


It is funny, I have done quite a lot of work for banks
- and none of them used 'Bankers Rounding'

The most important thing was to keep 'symetrical rounding'
eg: 2,000,000.5 and -2,000,000.5
both round to 2,000,001 regardless of sign

To accountants and bankers the minus sign just means 'Debit'
- one can Debit a Credit Account or Credit a Debit account
all that happens is that the +/- signs are reversed

The only really peculiar rules I ran into were OATs (French Treasury
Bonds) where one rounded the interest per bond to 3 dp before
multiplying by the number of bonds
.... and Jap bonds where the unit interest is truncated to 7dp before
multiplying.
( something to do with grotty calculators I suspect )

Jul 17 '05 #3

"Rick Rothstein" <ri************ @NOSPAMcomcast. net> wrote in message
news:v-*************** *****@comcast.c om...
By any chance, anyone got a rounding routine that does a work around

the VB
"round" bug?
I still find it amazing that a company as large as Microsoft would put

out a
math package that is so problematic.


It's not a bug, just (in my opinion) a dumb decision on their part.
Anyway, use the Format function (without a format specifier) as it uses
"normal" rounding rather than the Banker's Rounding (which you are
referring to as a "bug"). For example,

Debug.Print Format(2.5)

Rick - MVP

Thanks Rick..

No I am not referring to banker's rounding, I am referring to VB internal
lousy floating point errors.

VB's ailment in its math function is caused because there are errors in the
underlying code. For example, if you do

? round (68.505,2)

you get 68.5, which is incorrect; for if you round 68.505 to two decimal
points you should get 68.51.

This is caused because the underlying code that computes:

? int(68.505 * 100 + .5)/100

gives 68.5!

A more serious error can be seen here:

? int(68.505 * 1000 + 5)
68509 !!! (should have been 68510)

So the bigger the multiplication factor of a fraction, the larger the error
(the above error is a full ONE).

But that's OK, if no one have one ready, I'll write one and I'll post it /
share it with everyone else when done.
Jul 17 '05 #4
> No I am not referring to banker's rounding, I am referring to VB
internal
lousy floating point errors.

VB's ailment in its math function is caused because there are errors in the underlying code. For example, if you do

? round (68.505,2)

you get 68.5, which is incorrect; for if you round 68.505 to two decimal points you should get 68.51.

This is caused because the underlying code that computes:

? int(68.505 * 100 + .5)/100

gives 68.5!

A more serious error can be seen here:

? int(68.505 * 1000 + 5)
68509 !!! (should have been 68510)

So the bigger the multiplication factor of a fraction, the larger the error (the above error is a full ONE).

But that's OK, if no one have one ready, I'll write one and I'll post it / share it with everyone else when done.


The error you are describing should be endemic to all programming
languages. The problem is most floating point numbers in a decimal
system do not have **exact** representations in the underlying binary
number system that computers use. The number 68.505 does not have an
exact binary equivalent for a given number of bits (32 for VB), so any
language must approximate the value. Part of the problem is VB hides
this approximation from you. Although a number of type Double can
contain 16 digits, VB only shows you 15 of them (keeping the 16th one
hidden as a "guard" digit and using it for display rounding purposes).
You can see this by doing the following Print statement in the Immediate
window...

Print 68.505 - 68

You won't get the 0.505 that you expect; rather, VB will expose the
"guard" digit and print

0.5049999999999 95

I repeat, this is a problem in any language dealing with floating point
numbers (unless it is using an "integerize d" decimal system such when
using VB's Currency data type). You can read more on this problem
here....

INFO: Visual Basic and Arithmetic Precision
http://support.microsoft.com/default...NoWebContent=1

(Complete) Tutorial to Understand IEEE Floating-Point Errors
http://support.microsoft.com/default...NoWebContent=1

Anyway, you can still use the Format function to handle this problem.
For example (DP is not of decimal places to round to)...

DP = 2
Number = 68.505
Print Format(Number, "0." & String$(DP, "0"))

will print 68.51 as expected. Also

DP = 0
Number = 68.505 * 1000 + 5
Print Format(Number, "0." & String$(DP, "0"))

will print 68510 as expected.

Rick - MVP

Jul 17 '05 #5

"Raoul Watson" <Wa*****@Intell igenCIA.com> wrote in message
news:0f******** ********@nwrdny 03.gnilink.net. ..

"Rick Rothstein" <ri************ @NOSPAMcomcast. net> wrote in message
news:v-*************** *****@comcast.c om... VB's ailment in its math function is caused because there are errors in the underlying code. For example, if you do

? round (68.505,2)

you get 68.5, which is incorrect; for if you round 68.505 to two decimal points you should get 68.51.

This is caused because the underlying code that computes:

? int(68.505 * 100 + .5)/100

gives 68.5!


I have not used the round function, but I do a lot of this sort of
rounding with floating point numbers. Most of the problem goes away if
you use CLng() instead of Int().

Int() truncates, and often floating point numbers are slightly less than
they seem, i.e. 6850.5 + .5 seems to us to be exactly 6851, but may
internally be 6850.9999999999 , which truncates to 6850.

CLng(), on the other hand,round off the fractions:
?CLng(68.505 * 100 + .5)/100
68.51

So a function like this might work for you:

Public Function MyRound(ByVal Num As Double, ByVal Place As Long) As
Double
Dim X As Double
Dim Y As Double

If Place = 0 Then
MyRound = CLng(Num)
Else
X = 10 ^ Place
Y = Sgn(Num) / 2
MyRound = CLng(Num * X + Y) / X
End If

End Function

Jul 17 '05 #6
> I have not used the round function, but I do a lot of this sort of
rounding with floating point numbers. Most of the problem goes away if
you use CLng() instead of Int().

Int() truncates, and often floating point numbers are slightly less than they seem, i.e. 6850.5 + .5 seems to us to be exactly 6851, but may
internally be 6850.9999999999 , which truncates to 6850.

CLng(), on the other hand,round off the fractions:
?CLng(68.505 * 100 + .5)/100
68.51


CLng uses Banker's Rounding, so your function **can** make mistakes. The
easiest place to see this is with the following...

Print MyRound(68.5, 0)

which will print 68. On the other hand,

Print MyRound(69.5, 0)

will print 70.

Rick - MVP

Jul 17 '05 #7

"Rick Rothstein" <ri************ @NOSPAMcomcast. net> wrote in message
news:S-*************** *****@comcast.c om...
CLng uses Banker's Rounding, so your function **can** make mistakes. The easiest place to see this is with the following...

Print MyRound(68.5, 0)

which will print 68. On the other hand,

Print MyRound(69.5, 0)

will print 70.

Rick - MVP


Assuming you consider Banker's Rounding a mistake... <g>

Jul 17 '05 #8
> > CLng uses Banker's Rounding, so your function **can** make mistakes.
The
easiest place to see this is with the following...

Print MyRound(68.5, 0)

which will print 68. On the other hand,

Print MyRound(69.5, 0)

will print 70.

Rick - MVP


Assuming you consider Banker's Rounding a mistake... <g>


Oh, it's a mistake alright; no question in my mind on that.<g> I was a
practicing Civil Engineer for more than 32 years and, trust me, we NEVER
used Banker's Rounding in ANY of our calculations. Even Microsoft is not
entirely sure whether Banker's Rounding is a good thing to use in its
various programs or not...

http://support.microsoft.com/default...;EN-US;Q196652

Rick

Jul 17 '05 #9

"Rick Rothstein" <ri************ @NOSPAMcomcast. net> wrote in message
news:S-*************** *****@comcast.c om...
CLng uses Banker's Rounding, so your function **can** make mistakes. The easiest place to see this is with the following...

Print MyRound(68.5, 0)

which will print 68. On the other hand,

Print MyRound(69.5, 0)

will print 70.

Rick - MVP


Even worse:
?MyRound(32.75, 2)
32.76
Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1734
by: dhildebrandt | last post by:
I have a query that uses the Round function to change decimals into whole numbers. For 4 years straight the thing has always rounded the numbers in exactly the same way so that whenever I update my graphs, the percentages for the historical data are always exactly what they were the last time I ran the query. Now, for the first time my percentages are all just slightly off and I have not changed the query. The only thing I can think of...
4
7833
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. I need it to contain 180.26. Any ideas?
8
2086
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are they thinking??? I just don't
11
6656
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding seems to be done like this 3.435 = 3.44 3.445 = 3.44 Dim decNbr1 As Decimal
4
3495
by: AMDRIT | last post by:
Given: ?System.Math.Round(161.5D,0) 162.0 ?System.Math.Round(162.5D,0) 162.0 ?System.Math.Round(163.5D,0) 164.0 In either C# '05 or VB.Net '05, can anyone explain the inconsistancy? Is
11
15012
by: The Cool Giraffe | last post by:
I have trouble making the following line compile. int a = round (2.6); I have, of course, included <cmathand i can go flooring and ceiling with no problems. However, the rounding seems not to
18
2230
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if an item is 41.87 i want the application to bring it up to 41.89 if an item is 41.84 i want the application to round down to 41.79 does anyone know how i could do this?
206
13302
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) It then updates the value with numberOfPrecisions after the decimal
20
5010
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
0
9566
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9389
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10149
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10003
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9828
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7370
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5410
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3529
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2797
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.