Formatting Numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am running into a weird problem. I am using the following code:

String.Format("{0:0}:{1:00}:{2:00}", Hours.ToString, Minutes.ToString,
Seconds.ToString)

Just say seconds is set to 1, it should return a string that displays
0:00:01, but it isn't display that, instead I get 0:0:1. Can anyone tell me
why the format function isn't displaying both digit placeholders? 00 as a
format should return 01, not 1. Any ideas?
 
Mark said:
I am running into a weird problem. I am using the following code:

String.Format("{0:0}:{1:00}:{2:00}", Hours.ToString, Minutes.ToString,
Seconds.ToString)

Just say seconds is set to 1, it should return a string that displays
0:00:01, but it isn't display that, instead I get 0:0:1. Can anyone tell
me
why the format function isn't displaying both digit placeholders? 00 as a
format should return 01, not 1. Any ideas?

Remove the 'ToString'. Formatting will work with /numbers/, not /strings/.
 
Mark,
In addition to the other comments.

Rather then try to format hour, minute, second variables as independent
variables I find it easier to use DataTime & TimeSpan variables. As they
provide built-in formatting as well as reduce the number of variables I am
using.

Something like:

Dim hours As Integer = 0
Dim minutes As Integer = 0
Dim seconds As Integer = 1

Dim theTime As New TimeSpan(hours, minutes, seconds)
Dim s As String

s = theTime.ToString()

Note TimeSpan itself only supports a fixed format, I will convert a TimeSpan
into a DateTime if I need custom formatting.

s = String.Format("{0:H:mm:ss}", DateTime.MinValue.Add(theTime))



For details on custom datetime formats see:

http://msdn.microsoft.com/library/d...s/cpguide/html/cpcondatetimeformatstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconformattingtypes.asp


Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top