Round

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Douglas Guest

## Round

Hi All,

How do I force the Round function to return a number with the specified
number of decimal places?

e.g.:
Num1 = 2.34
Round Num1,1 '2.3

Num2 = 2
Round Num2,1 '2.0

Thanks,
Doug

2. Rick Rothstein Guest

## Re: Round

Round is a function; you are showing it being used as a subroutine. If you
want to permanently change the value assigned to a Single or Double
variable, you have to assign the Round(ed) value back into itself. For your
first example

Num1 = 2.34
Num1 = Round(Num1,1)

after which Num1 value 2.3 will be stored in it (the .04 portion is
permanently lost). Now your second example for Num2 seems to indicate that
you think a Round(ed) value will retain the specified number of decimal
places for display purposes. Numbers don't work that way. A number always
displays the number of decimal places that are *significant*. If there is no
decimal portion, then none is display if you print the number out. So

Num2 = 2
Num2 = Round(Num2, 1)

will still display 2 as its value. If you want to display a number with a
certain number of decimal place, use the Format\$ function (which can be show
with the \$ sign to return a String and without it to return a Variant). For

Print Format\$(Num2, "0.0")

will print 2.0 on the display. You can also use Format\$ to assign a
formatted number to a String variable. Such as

Dim SomeStringVariable As String
SomeStringVariable = Format\$(Num2, "0.0")

If you do this, the String variable will retain the forced decimal portion,
but that is because Strings are not numbers, rather they are grouped
characters. The Format\$ function can also be used to *show* a number

Print Format\$(Num1, "0.0")

will display 2.3 when printed out -- but the value of Num1 remains 2.34 and
is not permanently changed by the experience. However, if you use the
Variant form of Format and assign it back to the source variable, that
variable can be changed. The following results in the same changed value for
Num1 (from your first example) as shown above using Round:

Num1 = 2.34
Num1 = Format(Num1, "0.0")

So, you might ask . . . why do we have Round if Format can do the same
thing. Well, Round rounds differently than Format does. If you are rounding
a number up one place that ends in a 5, Format *always* rounds it up where
as Round rounds up *only* when the number in front of the 5 is an odd
number; otherwise, it rounds down (to the even number). This type of
rounding is sometimes called "Banker's Rounding" and has something to do
with minimizing the cumulative error that can result from using a long
series of rounded value in a calculation. For example, adding the
non-Banker's Rounded values of

2.45 rounds to 2.5
3.45 rounds to 3.5
4.45 rounds to 4.5
5.45 rounds to 5.5

sum to 15.8 and 16 respectively. Whereas the Banker's Rounding would add

2.4 + 3.5 + 4.4 + 5.5

to yield an exact 15.8 as an answer. Now this example was contrived to make
sure it added correctly when Banker's Rounding was used, but the idea is
that it reduces the upward bias that can occur when calculating with values
that end in 5 (and are being rounded up one place) and that are rounded
*before* they are used in calculations.

Although the documentation for it is not as prominent as it should be, you
can find out about Banker's Rounding in the Remarks section of the Type
Conversion Functions entry in VB's help files. (And while it seems to deal
only with rounding from one decimal place to an exact integer, it actually
deals with any number whose decimal part ends in 5 which is being rounded up
one decimal place.) Here is the quote from that section:

"When the fractional part is exactly 0.5, CInt and CLng always round it
to the nearest even number. For example, 0.5 rounds to 0, and 1.5
rounds to 2. CInt and CLng differ from the Fix and Int functions, which
truncate, rather than round, the fractional part of a number. Also, Fix
and Int always return a value of the same type as is passed in."

Rick

"Douglas" <hot_n_cold_2000@hotmail.com> wrote in message
news:39436783@news.devx.com...
> Hi All,
>
> How do I force the Round function to return a number with the specified
> number of decimal places?
>
> e.g.:
> Num1 = 2.34
> Round Num1,1 '2.3
>
> Num2 = 2
> Round Num2,1 '2.0
>
> Thanks,
> Doug
>
>

3. Douglas Guest

## Re: Round

Thanks! Brill!!

Later,
Doug

"Rick Rothstein" <rick_newsgroup@email.com> wrote in message
news:3943a03c\$1@news.devx.com...
> Round is a function; you are showing it being used as a subroutine. If you
> want to permanently change the value assigned to a Single or Double
> variable, you have to assign the Round(ed) value back into itself. For

your
> first example
>
> Num1 = 2.34
> Num1 = Round(Num1,1)
>
> after which Num1 value 2.3 will be stored in it (the .04 portion is
> permanently lost). Now your second example for Num2 seems to indicate that
> you think a Round(ed) value will retain the specified number of decimal
> places for display purposes. Numbers don't work that way. A number always
> displays the number of decimal places that are *significant*. If there is

no
> decimal portion, then none is display if you print the number out. So
>
> Num2 = 2
> Num2 = Round(Num2, 1)
>
> will still display 2 as its value. If you want to display a number with a
> certain number of decimal place, use the Format\$ function (which can be

show
> with the \$ sign to return a String and without it to return a Variant).

For
>
> Print Format\$(Num2, "0.0")
>
> will print 2.0 on the display. You can also use Format\$ to assign a
> formatted number to a String variable. Such as
>
> Dim SomeStringVariable As String
> SomeStringVariable = Format\$(Num2, "0.0")
>
> If you do this, the String variable will retain the forced decimal

portion,
> but that is because Strings are not numbers, rather they are grouped
> characters. The Format\$ function can also be used to *show* a number
> rounded. In your first example,
>
> Print Format\$(Num1, "0.0")
>
> will display 2.3 when printed out -- but the value of Num1 remains 2.34

and
> is not permanently changed by the experience. However, if you use the
> Variant form of Format and assign it back to the source variable, that
> variable can be changed. The following results in the same changed value

for
> Num1 (from your first example) as shown above using Round:
>
> Num1 = 2.34
> Num1 = Format(Num1, "0.0")
>
> So, you might ask . . . why do we have Round if Format can do the same
> thing. Well, Round rounds differently than Format does. If you are

rounding
> a number up one place that ends in a 5, Format *always* rounds it up where
> as Round rounds up *only* when the number in front of the 5 is an odd
> number; otherwise, it rounds down (to the even number). This type of
> rounding is sometimes called "Banker's Rounding" and has something to do
> with minimizing the cumulative error that can result from using a long
> series of rounded value in a calculation. For example, adding the
> non-Banker's Rounded values of
>
> 2.45 rounds to 2.5
> 3.45 rounds to 3.5
> 4.45 rounds to 4.5
> 5.45 rounds to 5.5
>
> sum to 15.8 and 16 respectively. Whereas the Banker's Rounding would add
>
> 2.4 + 3.5 + 4.4 + 5.5
>
> to yield an exact 15.8 as an answer. Now this example was contrived to

make
> sure it added correctly when Banker's Rounding was used, but the idea is
> that it reduces the upward bias that can occur when calculating with

values
> that end in 5 (and are being rounded up one place) and that are rounded
> *before* they are used in calculations.
>
> Although the documentation for it is not as prominent as it should be, you
> can find out about Banker's Rounding in the Remarks section of the Type
> Conversion Functions entry in VB's help files. (And while it seems to deal
> only with rounding from one decimal place to an exact integer, it actually
> deals with any number whose decimal part ends in 5 which is being rounded

up
> one decimal place.) Here is the quote from that section:
>
> "When the fractional part is exactly 0.5, CInt and CLng always round

it
> to the nearest even number. For example, 0.5 rounds to 0, and 1.5
> rounds to 2. CInt and CLng differ from the Fix and Int functions,

which
> truncate, rather than round, the fractional part of a number. Also,

Fix
> and Int always return a value of the same type as is passed in."
>
>
> Rick
>
>
> "Douglas" <hot_n_cold_2000@hotmail.com> wrote in message
> news:39436783@news.devx.com...
> > Hi All,
> >
> > How do I force the Round function to return a number with the specified
> > number of decimal places?
> >
> > e.g.:
> > Num1 = 2.34
> > Round Num1,1 '2.3
> >
> > Num2 = 2
> > Round Num2,1 '2.0
> >
> > Thanks,
> > Doug
> >
> >

>
>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise