Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use padleft for negative integers?

I need to add leading zeros to numbers which may be negative or positive. I am able to add them fine for positive numbers. How do I do the same for negative numbers?

For example:

int number1 = 1;
string pad1 = number1.ToString().PadLeft(3, '0');
//output 001
int number2 = -1;            
string pad2 = number2.ToString().PadLeft(3, '0');
//output 0-1
//expected output -001
like image 635
Procrastinating Programmer Avatar asked Dec 07 '25 03:12

Procrastinating Programmer


1 Answers

Tried using this... It works!

int number1 = 1;
string pad1 = number1.ToString("000");
//output 001
int number2 = -1;
string pad2 = number2.ToString("000");
//output -001
like image 84
Procrastinating Programmer Avatar answered Dec 08 '25 19:12

Procrastinating Programmer