Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formatting number without rounding

Status
Not open for further replies.

pkailas

Programmer
Jun 10, 2002
555
0
0
US
Is there a function that will format a number such as 2.348 by dropping the 3rd digit after the decimal? I don't want this rounded up to 2.35 as if I did formatnumber(2.348, 2).
 
Probably not the best way, but you can try this:

Private Sub Command2_Click()
MsgBox NoRound(2.348)
End Sub

Private Function NoRound(dblValue As Double) As Double
Dim strTemp As String
NoRound = CDbl(Fix(dblValue) & "." & Left(100 * (dblValue - Fix(dblValue)), 2))
End Function
 
Heres another


Text1 = Fix(num * 100) / 100

David Paulson

 

Option Explicit

Private Sub Command1_Click()
Dim a As Double
a = TwoDecNoRound(3.248)
MsgBox a
End Sub

Private Function TwoDecNoRound(strNum) As Double
Dim char As Integer
char = InStr(1, strNum, ".")
Select Case char
Case 0
TwoDecNoRound = strNum
Case Else
TwoDecNoRound = Left(strNum, char + 2)
End Select
End Function
 
I use dpaulson's shown method.
You can easily put it in a proceedure and make it dynamic, passing the number of decimal places to show (Optional = 2)
and then multiplying the figure by

Function ChoppitOff (myNumber As Double, Optional iDigitsToShow As Integer = 2)
ChoppitOff = Fix(myNumber * 10 ^ iDigitsToShow)/10 ^ iDigitsToShow
End Function
 
Hi,

Whatch out with the dot evaluation. The whole story will be wrong if the decimal separator is ","

Tibi
 

Opps, forgot to ad the return value type (otherwise it's a variant)

Function ChopItOff(myNumber As Double, Optional iDigitsAfterDec As Integer = 2) As Double
ChopItOff = Fix(myNumber * 10 ^ iDigitsAfterDec) / 10 ^ iDigitsAfterDec
End Function
´
Also, the below will accept a number or string if it can be converted to a number - then you can plug in a textbox.text property directly:

Function ChopItOff([myNumber As Variant, Optional iDigitsAfterDec As Integer = 2) As Double
If Not IsNumeric(myNumber) Then Exit Function
myNumber = CDbl(myNumber)
ChopItOff = Fix(myNumber * 10 ^ iDigitsAfterDec) / 10 ^ iDigitsAfterDec
End Function

Dim myNumber As Double
myNumber = ChopItOff(Text1.Text)
Text1.Text= CStr(ChopItOff(Text1.Text))
 
>The whole story will be wrong if the decimal separator is ","

Correct. Therefore you do not evaluate a decimal seperator, or, at most, you query the system to find out what seperator is used and sticj it into a variable, and use the variable instead.
 
A slightly different -more flexible if more convolouted- variation on the theme:

Code:
Public Function basTrunc(MyVal As Variant, _
                         Optional DelimChr As String = ".", _
                         Optional NChar As Integer = 2)

    'Michael Red 6/5/2003 Tek-Tips thread222-567798 for pkailas

    'Sample Usage:
    '? basTrunc(3.1284)
    ' 3.12

    '? basTrunc("Michael Lee Red", " ", 0)
    'Michael


    Dim MyStr As String
    Dim MyParts() As String
    Dim rtnVal As Variant

    If (IsNumeric(MyVal)) Then
        MyStr = CStr(MyVal)
     Else
        MyStr = MyVal
    End If

    MyParts = Split(MyStr, DelimChr)

    rtnVal = MyParts(0) & DelimChr & Left(MyParts(1), NChar)

    If (IsNumeric(MyVal)) Then
        basTrunc = CDbl(rtnVal)
     Else
        basTrunc = rtnVal
    End If

End Function



Pleas also note the complete lack of any error checking!





MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top