+ Reply to Thread
Results 1 to 9 of 9

How about truncating a decimal as the Trunc() function does?

  1. #1
    Registered User
    Join Date
    09-22-2004
    Posts
    35

    How about truncating a decimal as the Trunc() function does?

    Anyone know how to truncate a decimal off using code?(can't round)

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Skrimpy,

    To workaround this problem requires using strings. The code sample shows how to truncated without rounding.

    'N is the Decimal Number (whole or fraction) to truncate
    NbrStr = Str(N)
    I = InStr(1, NbrStr, ".")
    N = Val(Left(NbrStr, X - 1))


    Where there is a will, there is a way,
    Leith Ross

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why not just use the Int function? It returns the integer part of a number

    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    05-02-2004
    MS-Off Ver
    2007
    Posts
    43
    Definitely use Int, you could do this:


    Please Login or Register  to view this content.
    TJ

    PS You could also just use Application.WorksheetFunction.Trunc

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello,

    The best and easiest method is to use the ROUND worksheet function...

    Result = WorksheetFunction.Round(Number, Significant Digits)

    This can be used with the worksheet formulas...
    =ROUND(Number, Significant Digits)

    Just another way to do it,
    Leith Ross

  6. #6
    Registered User
    Join Date
    05-02-2004
    MS-Off Ver
    2007
    Posts
    43
    But, Round() does not truncate, it rounds!

    TJ

  7. #7
    Registered User
    Join Date
    09-22-2004
    Posts
    35
    Quote Originally Posted by tinyjack
    Definitely use Int, you could do this:

    .......

    PS You could also just use Application.WorksheetFunction.Trunc
    I tried this. It was not recognized. Is this another one of those functions that aren't available unless you go to tools and add-ins, like the "Analysis Tool Pak"?

  8. #8
    Registered User
    Join Date
    05-02-2004
    MS-Off Ver
    2007
    Posts
    43
    Well, I have to admit I did not try it and much to my surprise it is not supported by Application.WorksheetFunction. Learn something new everyday.

    I trust the rest of the post has solved your problem.

    TJ

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Int is a VBA function.

    And INT is a worksheet function that, as far as I know is available without any add-ins needed.

    I think that is not supported by WorkSheetFunction is because it is a VBA function.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1