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?
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"
where d is your variable or expressionDebug.Print Format(d, "#.000")
Thank you, Jeff!
When using that code within VBA, I get a compile error on Format. Does it need something else included?
No. What is your compile error? Can you show the code?
Here is the exact code I used to confirm it.
output:Private Sub test() Dim d As Double d = 3.4 Debug.Print "The number is " & Format(d, "#.000") End Sub
The number is 3.400
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
Nevermind. I see the problem. I have two variables with the same name.
Yes, Format is a built-in function. It is not a good idea to name variables after any predefined values or built-in functions.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks