Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String format options for int in .NET

Tags:

string

c#

I want to convert an int to a string but if it is a single digit I want to add a leading 0

Is that one of the built in format options? or do I have to do it manually?

like image 915
Sruly Avatar asked Apr 11 '10 08:04

Sruly


People also ask

What is format string in C#?

In C#, Format() is a string method. This method is used to replace one or more format items in the specified string with the string representation of a specified object.In other words, this method is used to insert the value of the variable or an object or expression into another string.

What is %s in string format?

%s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string.


1 Answers

This can be achieved with normal string formats:

3.ToString("00");

Will result in the string "03".

See custom numeric format strings on MSDN and their outputs.

like image 172
Oded Avatar answered Sep 23 '22 06:09

Oded