+ Reply to Thread
Results 1 to 8 of 8

Formatting numerical output using Debug.Print [in code]

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Formatting numerical output using Debug.Print [in code]

    I want to display 3 decimal places when outputting the value of a double even if it has trailing zeroes. For instance, if I have s = 3.400 and I use Debug.Print(s), I only get 3.4 displayed. How can I format the output?
    Last edited by 6StringJazzer; 11-01-2018 at 02:29 PM. Reason: Updated title to workaround forum bug when last word of subject is "print"

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,758

    Re: Formatting numerical output using Debug.Print [in code]

    Debug.Print  Format(d, "#.000")
    where d is your variable or expression
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Formatting numerical output using Debug.Print [in code]

    Thank you, Jeff!

  4. #4
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Formatting numerical output using Debug.Print [in code]

    When using that code within VBA, I get a compile error on Format. Does it need something else included?

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,758

    Re: Formatting numerical output using Debug.Print [in code]

    No. What is your compile error? Can you show the code?

    Here is the exact code I used to confirm it.

    Private Sub test()
    
       Dim d As Double
       
       d = 3.4
       Debug.Print "The number is " & Format(d, "#.000")
       
    End Sub
    output:
    The number is 3.400

  6. #6
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Formatting numerical output using Debug.Print [in code]

    Really odd. I copied your line, but I'm using my variable which is a double and it changes the word Format to format and then doesn't compile. Here is my code...

    Option Explicit
    
    Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
    Sub Timer()
       
    Dim started As Long
    Dim ended   As Long
    Dim i       As Long
    Dim s       As Double
    Dim m       As Long
    Dim h       As Long
    Dim format  As Boolean
    
    h = 0
    m = 0
    format = True
    started = timeGetTime ' Get milliseconds since startup
        For i = 1 To 100
            Cells(1, 1) = 4
        Next
    ended = timeGetTime
    
    'If format is true, display results using h m s.xxx otherwise display as ms
    s = ended - started
    If format Then
        If s >= 3600000 Then
            h = s / 3600000
            m = (s Mod 3600000) / 60000
            s = (s Mod 3600000) Mod 6000
            s = s / 1000
        ElseIf s >= 60000 Then
            m = s / 60000
            s = s Mod 60000
            s = s / 1000
        Else
            s = s / 1000
        End If
        
        'Debug.Print "Time Taken = " & h & "h " & m & "m " & s & "s"
        Debug.Print "The number is " & format(s, "#.000")
    Else
        Debug.Print "Time Taken = " & s & "ms"
    End If
    
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    10-12-2018
    Location
    Leesburg, FL
    MS-Off Ver
    2013
    Posts
    113

    Re: Formatting numerical output using Debug.Print [in code]

    Nevermind. I see the problem. I have two variables with the same name.

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,758

    Re: Formatting numerical output using Debug.Print [in code]

    Yes, Format is a built-in function. It is not a good idea to name variables after any predefined values or built-in functions.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Debug.Print Error
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-15-2014, 11:10 PM
  2. What does Debug.Print do?
    By KRAND in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-12-2013, 04:39 AM
  3. Excluding formatting from print output
    By JohnnyCash in forum Excel General
    Replies: 7
    Last Post: 02-18-2010, 08:04 AM
  4. How to format TextBox output as numerical
    By knarf in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-15-2009, 02:52 PM
  5. Debug.Print Problem
    By jhl84 in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 09-24-2007, 04:07 AM
  6. how to debug and get an output
    By sa02000 in forum Excel General
    Replies: 1
    Last Post: 12-14-2006, 12:35 AM
  7. Replies: 1
    Last Post: 08-24-2005, 04:05 PM

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