Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi,

I have a int value represented as string to represent hour and minute. If i get minute as 0 i must represent as 00.. pls help
Posted
Comments
Menon Santosh 23-Mar-13 4:59am    
Post your code

Try
C#
string formatted = string.Format("{0:D2}", 0);
The "D" specified indicates you want a fixed size integer, the 2 says "a minimum of two digits, use leading zeros if needed"
http://msdn.microsoft.com/en-gb/library/dwhawy9k.aspx[^]
 
Share this answer
 
Comments
Arjun Menon U.K 23-Mar-13 4:24am    
Yes Griff, i tried the method you told...

objtempSchedule.StartMinute = objBWTemp.StartMinute;

the entity i try to set are both strings. I tried this objtempSchedule.StartMinute = objBWTemp.StartMinute.ToString("D2"); but its showing error "No actual conversion is performed"
OriginalGriff 23-Mar-13 5:30am    
Ah! It is possible to pad a string with leading zeros (you add the minimum number to the front of the string, and then take the last "n" characters with substring) but it's a messy job - it would be better to use a number in the first place!
I would either provide the numeric value as an int from my objBWTemp class (either instead of or as well as a string) or convert it to an int then format it for display:

int val = int.Parse(objBWTemp.StartMinute);
objtempSchedule.StartMinute = val.ToString("D2");

However, I would be very tempted to use an integer for StartMinute on the objTempSchedule class as well, and let it sort out the formatting itself.
Arjun Menon U.K 23-Mar-13 5:41am    
the reason i gave string is that the entity is set from a xml response (from a web service) and i found that int values passed over webservice have value maintaining problem. Since i don't know much about it i gave all entity values as string.
Assuming you mean Visual Studio and .Net, have a look at String.Format[^]
 
Share this answer
 

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



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