Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hi,
I use String.format method in Java,it works well.for example:

Java Example:
Java
int min=6;
int sec=12;
string time;
time=String.format("%2d:%2d",min,sec);
System.out.println(time); // Output:  06:12



but in C# this method does not work.

C# example:
C#
int min=6;
int sec=12;
string time;
time=String.Format("%2d:%2d",min,sec);
Console.Write(time); //Output: %2d:%2d

why?
how can i change above method in order to see same result as java?

thanks in advance
Posted
Comments
[no name] 16-Jul-13 9:34am    
"why?", because java != C#?

Don't know if this is what you mean? Otherwise, you will have to explain a little more...

C#
time=String.Format("{0}:{1}",min,sec);


or if you need the exact format:

C#
time=String.Format("{0}:{1}",min.ToString().PadLeft(2,'0'),sec.ToString().PadLeft(2,'0'));

[Edit: Matt Heffron]
Use format strings:
C#
time = String.Format("{0:D2}:{1:D2}", min, sec);
 
Share this answer
 
v3
try this
but you have to convert it to a datetime before like this
C#
DateTime dt = new DateTime(2007, 11, 1,min,sec);

and then just pick the time
C#
time=String.Format("{0:t}", dt);  
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900