Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Any rounding functions?
Message
From
10/05/1999 14:58:28
 
 
To
10/05/1999 10:22:30
General information
Forum:
Visual Basic
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00216716
Message ID:
00216837
Views:
30
>Any rounding functions provided by VB5 or VB6?
>eg. round 5.1245 to 5.125 if I want to round to 3 decimal place?
>Or do I need to write it myself?

I faced the same situation a while ago. In my case, it was a little bit more difficult as I wanted to format as well, like the TRANSFORM() function. So, I came up with FORMATNUMBER() and PADR() functions. From here, you should be able to find your way.
'-------------------------------------------------------------------------------
' There is no format option, set let build our own
' expN1 Maximum length of the value
' expN2 Value
' expN3 Number of decimal places
'-------------------------------------------------------------------------------
Private Function FormatNumber(tnLength, tnValue, tnDecimal)

Dim lcValue As String
Dim lnPosition As Integer
Dim lcFormat As String

' Converts the numeric value into a character
lcValue = CStr(tnValue)

' Try to find the decimal location
lnPosition = InStr(lcValue, ".")

' If there is no decimal, the formula would be a little different than if we have a decimal
If lnPosition = 0 Then
   lcFormat = Space(tnLength - Len(lcValue) - tnDecimal) + Format(tnValue, "#0")
   Else
   lcFormat = Space(tnLength - lnPosition - tnDecimal) + Format(tnValue, "#0." + PadR("", tnDecimal, "0"))
End If

' Returns the value
FormatNumber = lcFormat

End Function


'-------------------------------------------------------------------------------
' There is no Pad right function so let build our own
' expC1 String
' expN1 Length of the string
' expC2 Pad character
'-------------------------------------------------------------------------------
Private Function PadR(tcString, tnLength, tcCharacter)

Dim lnCounter
Dim lcString

lcString = ""
For lnCounter = 1 To tnLength - Len(tcString)
   lcString = lcString + tcCharacter
Next
PadR = lcString + tcString

End Function
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Reply
Map
View

Click here to load this message in the networking platform