Ishan
Ishan

Reputation: 4028

padding in asp.net

Is there a function to append padding digits to the left of a digit.I want to form a 3 digit number, so when the value is '3' i need to make it '003'

e.g in php we have

$m_ccode=str_pad($m_casecode,3,"0",str_pad_left);

same i want to convert in asp.net c#. How can i do it, i have numeric value stored in string 's'

String s = DropDownList3.SelectedItem.Value;

Upvotes: 1

Views: 714

Answers (2)

Albin Sunnanbo
Albin Sunnanbo

Reputation: 47038

string variant1 = "3".PadLeft(3, '0'); // if you have a string
string variant2 = 3.ToString("000"); // if you have a number

In your case you need to unbox your int if the numeric value is an int

String s = ((int)DropDownList3.SelectedItem.Value).ToString("000");

Upvotes: 1

Pieter van Ginkel
Pieter van Ginkel

Reputation: 29632

Try the following:

String s = DropDownList3.SelectedItem.Value.PadLeft(3, '0');

Upvotes: 1

Related Questions