5 Replies - 9338 Views - Last Post: 28 May 2010 - 06:48 AM

#1 irtaza   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 25-June 04

RoundUp and RoundDown functions in VB 6.0

Posted 14 April 2005 - 01:42 AM

Description: Two simple functions that are not found in VB. Suppose you have a number '3.376'. Now when you will use the RoundUp function you will get the result as '4.0'. By using RoundDown function the result would be '3.0'. However, for .NET you could use the Math.Floor and Math.Ceiling methods
Public Function roundDown(dblValue As Double) As Double
On Error GoTo PROC_ERR
Dim myDec As Long

myDec = InStr(1, CStr(dblValue), ".", vbTextCompare)
If myDec > 0 Then
    roundDown = CDbl(Left(CStr(dblValue), myDec))
Else
    roundDown = dblValue
End If

PROC_EXIT:
    Exit Function
PROC_ERR:
    MsgBox Err.Description, vbInformation, "Round Down"
End Function

Public Function roundUp(dblValue As Double) As Double
On Error GoTo PROC_ERR
Dim myDec As Long

myDec = InStr(1, CStr(dblValue), ".", vbTextCompare)
If myDec > 0 Then
    roundUp = CDbl(Left(CStr(dblValue), myDec)) + 1
Else
    roundUp = dblValue
End If

PROC_EXIT:
    Exit Function
PROC_ERR:
    MsgBox Err.Description, vbInformation, "Round Up"
End Function



Is This A Good Question/Topic? 0
  • +

Replies To: RoundUp and RoundDown functions in VB 6.0

#2 MikoMia   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 09-January 08

Re: RoundUp and RoundDown functions in VB 6.0

Posted 09 January 2008 - 12:52 AM

*O*M*G* Ever heard of the build in Int function?! To Round Down, use Int(), as in: Dim n as Double, floor as Double n = 3.99745823 floor = Int(n) To Round Up, simply use If Int(number)<>number Then ceil = Int(number) + 1 Else ceil = number End If oh.. and this code is about 1000x faster..
Was This Post Helpful? 0
  • +
  • -

#3 Mordy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 08-September 08

Re: RoundUp and RoundDown functions in VB 6.0

Posted 08 September 2008 - 05:39 AM

Won't anything from 3.50 to 3.99 be rounded up to 4 if you use the built-in INT function? I think that's why he made this function.
Was This Post Helpful? 0
  • +
  • -

#4 AndyEsser   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 25-July 08

Re: RoundUp and RoundDown functions in VB 6.0

Posted 04 November 2008 - 08:06 AM

You have a problem with this function if you start having numbers so small they start getting displayed as standard form, ie 1.66e-2 since it checks for the decimal point and does go by the value.
Was This Post Helpful? 0
  • +
  • -

#5 richmassena   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 22-December 08

Re: RoundUp and RoundDown functions in VB 6.0

Posted 22 December 2008 - 10:04 AM

you really must be joking. Parsing the string representation of a number to round it up. Try something like this: newnumber=int(number+.5)
Was This Post Helpful? 0
  • +
  • -

#6 deltarock   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 28-May 10

Re: RoundUp and RoundDown functions in VB 6.0

Posted 28 May 2010 - 06:48 AM

Try this: Function RoundUpDown(dblNumber As Double, intDecimalPlaces As Integer, _ Optional varUpDown As Variant) As Double Dim Multiplier As Long, New_no As Double, Dec_no As Double, IncreaseBy As Integer On Error GoTo Error_found Multiplier = Val("1" & RepeatIT("0", intDecimalPlaces)) New_no = dblNumber * Multiplier Dec_no = New_no - Int(New_no) IncreaseBy = IIf(Dec_no > 0, 1, 0) If IsMissing(varUpDown) Then ' round up GoTo RoundUp Else Select Case varUpDown Case 1 ' round up GoTo RoundUp Case 2 ' round down GoTo RoundDown Case "U" ' round up GoTo RoundUp Case "D" ' round down GoTo RoundDown Case "u" ' round up GoTo RoundUp Case "d" ' round down GoTo RoundDown Case "UP" ' round up GoTo RoundUp Case "DOWN" ' round down GoTo RoundDown Case "Up" ' round up GoTo RoundUp Case "Down" ' round down GoTo RoundDown Case "up" ' round up GoTo RoundUp Case "down" ' round down GoTo RoundDown Case Else ' round up GoTo RoundUp End Select End If RoundUp: RoundUpDown = (Int(New_no + IncreaseBy)) / Multiplier Exit Function RoundDown: RoundUpDown = Int(New_no) / Multiplier Exit Function Error_Exit: Exit Function Error_found: RoundUpDown = 0 End Function Function RepeatIT(text As String, number_times) Dim i Dim mytext As String mytext = "" For i = 1 To number_times mytext = mytext & text Next i RepeatIT = mytext End Function
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1