I have a number that I need to convert to a string. First I used this:
Key = i.ToString();
But I realize it's being sorted in a strange order and so I need to pad it with zeros. How could I do this?
I have a number that I need to convert to a string. First I used this:
Key = i.ToString();
But I realize it's being sorted in a strange order and so I need to pad it with zeros. How could I do this?
Rather simple:
Key = i.ToString("D2");
D
stands for "decimal number", 2
for the number of digits to print.
string.Format("D2", value)
you'll get D2
in the output. this doesn't work. We must use "{0:00}"
in this case.
string.Format("{0:D2}", value)
instead. Just saying string.Format("D2", ...)
won't replace anything no matter what since you don't have a placeholder.
Sep 11, 2015 at 19:25
value
is an int. If value
is a double, for instance, it will have to be string.Format("{0:00}", value)
Feb 23, 2017 at 15:02
System.FormatException
if the number is a decimal. Better to use .ToString("N0").PadLeft(2, '0')
. However, PadLeft
does not work properly with negative numbers (zeros in front of negative sumbol).
Feb 14 at 17:10
See String formatting in C# for some example uses of String.Format
Actually a better example of formatting int
String.Format("{0:00000}", 15); // "00015"
or use String Interpolation:
$"{15:00000}"; // "00015"
If you like to keep it fixed width, for example 10 digits, do it like this
Key = i.ToString("0000000000");
Replace with as many digits as you like.
i = 123
will then result in Key = "0000000123"
.
Since nobody has yet mentioned this, if you are using C# version 6 or above (i.e. Visual Studio 2015) then you can use string interpolation to simplify your code. So instead of using string.Format(...)
, you can just do this:
Key = $"{i:D2}";
use:
i.ToString("D10")
See Int32.ToString (MSDN), and Standard Numeric Format Strings (MSDN).
Or use String.PadLeft
. For example,
int i = 321;
Key = i.ToString().PadLeft(10, '0');
Would result in 0000000321
. Though String.PadLeft
would not work for negative numbers.
See String.PadLeft (MSDN).
For interpolated strings:
$"Int value: {someInt:D4} or {someInt:0000}. Float: {someFloat: 00.00}"
Try:
Key = i.ToString("000000");
Personally, though, I'd see if you can't sort on the integer directly, rather than the string representation.
Usually String.Format("format", object) is preferable to object.ToString("format"). Therefore,
String.Format("{0:00000}", 15);
is preferable to,
Key = i.ToString("000000");
int num=1;
string number=num.ToString().PadLeft(4, '0')
Output="00001"
EDIT: Changed to match the PadLeft amount
Here I want my no to limit in 4 digit like if it is 1 it should show as 0001,if it 11 it should show as 0011..Below are the code.
reciptno=1;//Pass only integer.
string formatted = string.Format("{0:0000}", reciptno);
TxtRecNo.Text = formatted;//Output=0001..
I implemented this code to generate Money receipt no.
I found a better way tried all the way but not worked for me
Convert.ToDecimal(LN).ToString("000000#");
LN is Int64