Didu
Didu

Reputation: 79

Convert string type to 8 digit in c#

I need to convert this value to 8 digit format. (example : 1 --> 00000001).any help? here's my code

foreach(DataRow dr in _dsGridCsv.Tables[0].Rows)
  {           
     byte empfrmat =byte.Parse(dr["emp_id"].ToString());
     csv += empfrmat;
     csv += "\r\n";
  }

Upvotes: 1

Views: 2109

Answers (3)

Jas
Jas

Reputation: 71

strValue.ToString("D8");

D8 means format as a decimal with up to 8 leading zeroes

Upvotes: 2

Lifewithsun
Lifewithsun

Reputation: 998

  1. By using padLeft

    eg. "1".PadLeft(8, '0');

Ref. Add zero-padding to a string

Upvotes: 1

sujith karivelil
sujith karivelil

Reputation: 29036

So you want to change the value to 8 digit format, according to the example that you specified, you can try the following code for this:

int value = 1;
String outputStr = value.ToString("00000000");
Console.WriteLine(outputStr) // this will print 00000001

Upvotes: 0

Related Questions