How do I round off decimals in VB? 
Author Message
 How do I round off decimals in VB?

Hi!
I'm writing a program that, among other things, uses decimal division and
multiplication. How can I in a simple way round off the answers (eg. 21.1461 ->
21.15)? Is there a simple function for this in Visual Basic (4.0)?

Greatful for answers....
/Nick



Sun, 19 Jul 1998 03:00:00 GMT  
 How do I round off decimals in VB?

Quote:

>Hi!
>I'm writing a program that, among other things, uses decimal division and
>multiplication. How can I in a simple way round off the answers (eg. 21.1461 ->
>21.15)? Is there a simple function for this in Visual Basic (4.0)?

>Greatful for answers....
>/Nick

Round() rounds to the nearest whole number. Of course you would first have to multiply by 100 and then dividid the
result by 100.


Sun, 19 Jul 1998 03:00:00 GMT  
 How do I round off decimals in VB?
: Hi!
: I'm writing a program that, among other things, uses decimal division and
: multiplication. How can I in a simple way round off the answers (eg. 21.1461
: -> 21.15)? Is there a simple function for this in Visual Basic (4.0)?

 Well, there is the abysmally slow Format$() function, if all you want is
to display a result (in a Label, say).  To get a rounded numeric answer,
the trick would be to multiply by 100, add 0.5, and use Int(), and then
divide the result by 100.



Sun, 19 Jul 1998 03:00:00 GMT  
 How do I round off decimals in VB?

Quote:
>Hi!
>I'm writing a program that, among other things, uses decimal division and
>multiplication. How can I in a simple way round off the answers (eg.
21.1461 ->
>21.15)? Is there a simple function for this in Visual Basic (4.0)?

>Greatful for answers....
>/Nick

Try this,

Dim dVal as double

dVal = 21.1461
dVal = CDbl(Format(dVal, "0.00"))

This should round the number to the second decimal place.

Hope this helps,
        Jeffrey J. Fleming
        MSN:            GalaxySoft
        AOL:            Trekker 1
        CompuServe:     102167,2656

        WWW:            http://users.aol.com/trekker1



Wed, 22 Jul 1998 03:00:00 GMT  
 How do I round off decimals in VB?

Quote:

> Hi!
> I'm writing a program that, among other things, uses decimal division and
> multiplication. How can I in a simple way round off the answers (eg. 21.1461
->
> 21.15)? Is there a simple function for this in Visual Basic (4.0)?

> Greatful for answers....
> /Nick

Don't know about VB 4.0 but in VB 3.0 you could use the Format function to
round : i.e

        x = format(x,"####0.00")
Steve.



Sun, 26 Jul 1998 03:00:00 GMT  
 How do I round off decimals in VB?

Quote:



>>> Hi!
>>> I'm writing a program that, among other things, uses decimal division and
>>> multiplication. How can I in a simple way round off the answers (eg. 21.1461
>>->
>>> 21.15)? Is there a simple function for this in Visual Basic (4.0)?

>>> Greatful for answers....
>>> /Nick

>>Don't know about VB 4.0 but in VB 3.0 you could use the Format function to
>>round : i.e

>>       x = format(x,"####0.00")
>>Steve.

>You might also try the CINT data type conversion function.  From the VB
>Help:
>Note    CInt differs from the Fix and Int functions, which truncate, rather
>than round, the fractional part of a number.  When the fractional part is
>exactly 0.5, the CInt function always rounds it to the nearest even
>number.  For example, 0.5 rounds to 0, and 1.5 rounds to 2.

In VB 4 there is a ROUND() function; two versions of it, in fact.  One
that goes to whole numbers and another that rounds to an arbitrary
number of decimals.

Following your example:
   ROUND(21.1461, 2) = 21.15

HTH

Paul DiRezze



Wed, 29 Jul 1998 03:00:00 GMT  
 How do I round off decimals in VB?

Quote:


>Subject: Re: How do I round off decimals in VB?
>Date: Sat, 10 Feb 1996 17:59:04 GMT




>>>> Hi!
>>>> I'm writing a program that, among other things, uses decimal division and
>>>> multiplication. How can I in a simple way round off the answers (eg. 21.1461
>>>->
>>>> 21.15)? Is there a simple function for this in Visual Basic (4.0)?

>>>> Greatful for answers....
>>>> /Nick

>>>Don't know about VB 4.0 but in VB 3.0 you could use the Format function to
>>>round : i.e

>>>       x = format(x,"####0.00")
>>>Steve.

>>You might also try the CINT data type conversion function.  From the VB
>>Help:
>>Note    CInt differs from the Fix and Int functions, which truncate, rather
>>than round, the fractional part of a number.  When the fractional part is
>>exactly 0.5, the CInt function always rounds it to the nearest even
>>number.  For example, 0.5 rounds to 0, and 1.5 rounds to 2.
>In VB 4 there is a ROUND() function; two versions of it, in fact.  One
>that goes to whole numbers and another that rounds to an arbitrary
>number of decimals.
>Following your example:
>   ROUND(21.1461, 2) = 21.15
>HTH
>Paul DiRezze


How about this:
Static Function Round (n As Variant) As Variant
 Dim r
 n = Format(n, "Fixed")
 r = Right(n, 1)
  Select Case r
   Case 1, 2: n = n - (r / 100)
   Case 3, 4: n = n + ((5 - r) / 100)
   Case 6, 7: n = n - ((r - 5) / 100)
   Case 8, 9: n = n + ((10 - r) / 100)
   Case Else
    Round = n
    Exit Function
  End Select
 Round = Format(n, "Fixed")
End Function
This will round beautifully to 2 decimal places.   Cheers


Fri, 31 Jul 1998 03:00:00 GMT  
 How do I round off decimals in VB?

Quote:



> >Subject: Re: How do I round off decimals in VB?
> >Date: Sat, 10 Feb 1996 17:59:04 GMT





> >>>> Hi!
> >>>> I'm writing a program that, among other things, uses decimal division and
> >>>> multiplication. How can I in a simple way round off the answers (eg. 21.1461
> >>>->
> >>>> 21.15)? Is there a simple function for this in Visual Basic (4.0)?

> >>>> Greatful for answers....
> >>>> /Nick

> >>>Don't know about VB 4.0 but in VB 3.0 you could use the Format function to
> >>>round : i.e

> >>>       x = format(x,"####0.00")
> >>>Steve.

> >>You might also try the CINT data type conversion function.  From the VB
> >>Help:

> >>Note    CInt differs from the Fix and Int functions, which truncate, rather
> >>than round, the fractional part of a number.  When the fractional part is
> >>exactly 0.5, the CInt function always rounds it to the nearest even
> >>number.  For example, 0.5 rounds to 0, and 1.5 rounds to 2.

> >In VB 4 there is a ROUND() function; two versions of it, in fact.  One
> >that goes to whole numbers and another that rounds to an arbitrary
> >number of decimals.

> >Following your example:
> >   ROUND(21.1461, 2) = 21.15

> >HTH

> >Paul DiRezze

> How about this:
> Static Function Round (n As Variant) As Variant
>  Dim r
>  n = Format(n, "Fixed")
>  r = Right(n, 1)
>   Select Case r
>    Case 1, 2: n = n - (r / 100)
>    Case 3, 4: n = n + ((5 - r) / 100)
>    Case 6, 7: n = n - ((r - 5) / 100)
>    Case 8, 9: n = n + ((10 - r) / 100)
>    Case Else
>     Round = n
>     Exit Function
>   End Select
>  Round = Format(n, "Fixed")
> End Function
> This will round beautifully to 2 decimal places.   Cheers

Deets Consulting

How about another example for VB 3.0 - returns whole number

Function Round (aNumber As Double) As Long
    Dim BaseNum As Long, Remainder As Long
    BaseNum = Fix(aNumber)
    'get fractional remainder and handle div by 0
    Select Case Abs(BaseNum)
        Case 0: Remainder = aNumber
        Case 1: Remainder = aNumber - BaseNum
        Case Else: Remainder = aNumber Mod BaseNum
    End Select
    If Abs(Remainder) >= .5 Then
        ' adjust value +\- 1
        BaseNum = BaseNum + Sgn(Remainder)
    End If
    Round = BaseNum
End Function



Sat, 01 Aug 1998 03:00:00 GMT  
 How do I round off decimals in VB?

Quote:
>> How about this:
>> Static Function Round (n As Variant) As Variant
>>  Dim r
>>  n = Format(n, "Fixed")
>>  r = Right(n, 1)
>>   Select Case r
>>    Case 1, 2: n = n - (r / 100)
>>    Case 3, 4: n = n + ((5 - r) / 100)
>>    Case 6, 7: n = n - ((r - 5) / 100)
>>    Case 8, 9: n = n + ((10 - r) / 100)
>>    Case Else
>>     Round = n
>>     Exit Function
>>   End Select
>>  Round = Format(n, "Fixed")
>> End Function
>> This will round beautifully to "2 decimal places".   Cheers

My apologies for the last statement.  That should read "to the nearest 5
cents"!  I put in the whole routine to demonstrate a couple of different
methods for handling numbers.  The original question is answered by using:
myNumber = Format(myNumber, "Fixed")  'this rounds to two decimal places.
Sorry if I confused anyone *grin*
Harry Strybos


Sun, 02 Aug 1998 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. rookie quesiton re: rounding off currency to two decimal places

2. rookie quesiton re: rounding off currency to two decimal places

3. rounding off decimals

4. Rounding-off question from VB newbie.

5. Rounding off values in VB

6. remove decimal or round up

7. Rounding to two decimal places (Access 97)

8. Round decimals

9. Need Function to Round Decimal

10. Rounding currency to 2 decimal digits.

11. Round Single Data Type to 2 decimal places

12. Variable with Single Data Type rounded to 2 decimal places

 

 
Powered by phpBB® Forum Software