Posted on 2006-05-09
Last Modified: 2012-08-13
  How to use format to chop off the long trail decimal without rounding (up and down ) it and up to 2 decimals only?

 e.g -  343.236788976
 to be  343.23 NOT 343.24

Question by:wzm
    LVL 12

    Expert Comment

    Hello, w2m,

    I would use:

        Dim strResult As String
        strResult = Format(dblValue, "#0.000")
        strResult = Left(strResult, Len(strResult) - 1)

    Another possibility would be:

        Format(Fix(100 * Value) / 100, "#0.00")

    "Fix" returns the integer equal to or closer to zero than the argument.  Depending on how you want to deal with negative numbers you might want to use "Int" instead of "Fix".  "Int" returns the integer less than or equal to the argument.  (The above will "truncate" -123.456 to -123.45 whereas if "Int" is used it will return -123.46.)  

    LVL 1

    Accepted Solution


    here's a little code snippet.
    the function is the main core here.
    all the other stuff is just for a nice demo for the output itself.

    note, however, that the function could be better (in terms of performance) if you have a predefined array of formats for the given argument DigitsAfter, instead of creating the correct format for each call. if you wish - please reply and i'll rewrite the code.

    best regards,


    Public Function DecimalFormat(Number As Double, DigitsAfter As Byte) As String
        Const MAX_DIGITS_ON_THE_RIGHT_SIDE As Byte = 10
        Dim strFormat As String
        Dim bytIndex As Byte
        Dim lngLength As Long
        'check that we're in a valid context. raise an error if not...
        If (DigitsAfter > MAX_DIGITS_ON_THE_RIGHT_SIDE) Then
            Err.Raise vbObjectError + 1, "DecimalFormat", "'DigitsAfter' cannot exceed " & CStr(MAX_DIGITS_ON_THE_RIGHT_SIDE)
        End If
        'special case of zero
        If (0 = DigitsAfter) Then
            DecimalFormat = Format(Number, "#")
            Exit Function
        End If

        'prepare the format string
        strFormat = "#."
        For bytIndex = 0 To DigitsAfter
            strFormat = strFormat & "0"

        DecimalFormat = Format(Number, strFormat)
        lngLength = Len(DecimalFormat)
        If (lngLength > 0) Then
            DecimalFormat = Left(DecimalFormat, lngLength - 1)
        End If
    End Function

    Private Sub Command1_Click()
        Dim dblDemoValue As Double
        Dim strTemp As String
        Dim bytIndex As Byte
        dblDemoValue = 123.456789
        bytIndex = 0
        strTemp = "DecimalFormat(" & dblDemoValue & ", " & bytIndex & ") = " & DecimalFormat(dblDemoValue, bytIndex)
        For bytIndex = 1 To 9
            strTemp = strTemp & vbNewLine & "DecimalFormat(" & dblDemoValue & ", " & bytIndex & ") = " & DecimalFormat(dblDemoValue, bytIndex)
        MsgBox strTemp
    End Sub

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    This article will show, step by step, how to integrate R code into a R Sweave document
    A short article about problems I had with the new location API and permissions in Marshmallow
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now