Padding integer with zeros [duplicate] - c#

This question already has answers here:
How can I format a number into a string with leading zeros?
(11 answers)
C# convert int to string with padding zeros?
(15 answers)
Closed 4 years ago.
I know I can format string with left padding with spaces with line:
int i=5;
serial = String.Format("{0,20}", i);
But how to left pad wit 0 in order to get string below?
00000000000000000005

You can use the built-in function PadLeft:
int i = 5;
var result = i.ToString().PadLeft(8, '0');
Note: this doesn't work for negative numbers (you'd get 00000000-5).
For that you can use the built-in formatters:
int i = 5;
var result = string.Format("{0:D8}", i);

Related

C#__ adding string into string using length [duplicate]

This question already has answers here:
.NET String.Format() to add commas in thousands place for a number
(23 answers)
Closed 2 years ago.
In my program, I'm trying to add a comma for a number that's length is more than 3. For example if number is 1000, it should output 1,000.
However I can't find out how to add on remaining numbers after I put comma into first one. The code below is what I've got:
// if the answer is more than 999
string answerThousand = Convert.ToString(lbl_NumResult.Text);
if (answerThousand.Length > 3)
{
lbl_NumResult.Text = answerThousand[1] + "," + answerThousand[ /* What must be here to add remaining numbers? */];
}
You could just pass a formatter to the ToString method:
decimal inputValue = 0;
if (decimal.TryParse(lbl_NumResult.Text, out inputValue))
{
string answerThousand = inputValue.ToString("N", CultureInfo.InvariantCulture);
}
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#NFormatString

c# Fill string with 0 at left [duplicate]

This question already has answers here:
Pad left with zeroes
(4 answers)
Closed 2 years ago.
I try to fill a string with 0 at left.
My variable :
string value = "0.5";
So my output is 0.5 and I want 00.5. This value can also be an integer like 450, needed as 0450.
I tried string.Format("{0:0000}", value); but it doesn't work.
I also tried string value = (double.Parse(value)).ToString("0000"); but it doesn't work.
You should use PadLeft method for that
var value = "0.5";
var result = value.PadLeft(4, '0'); //gives 00.5

how to convert int variables to string variables [duplicate]

This question already has answers here:
Convert int to string?
(12 answers)
Closed 4 years ago.
I am currently splitting a string from a textbox that the user will fill with three numbers. Those numbers i want to be saved as seperate integers. Any help as to how to do this? Thanks for any help!
string[] count = txtPoemInput.Text.Split('/'); //Splitting values for keyword numbers
int Poem, Line, Word;
count[0] = Poem.ToString; // Example
count[1] = Line; // Example
count[2] = Word;
Here is what you need to do. Use Convert.ToInt32
Poem = Convert.ToInt32(count[0]);
Line = Convert.ToInt32(count[1]);
Word = Convert.ToInt32(count[2]);

Return a string that is repeated a many times [duplicate]

This question already has answers here:
Is there an easy way to return a string repeated X number of times?
(21 answers)
How to repeat a set of characters
(3 answers)
Closed 4 years ago.
I want to insert a number of * before a string based on an items depth and I'm wondering if there is a way to return a string repeated Y times. Example:
string indent = "***";
Console.WriteLine(indent.Redraw(0)); //would print nothing.
Console.WriteLine(indent.Redraw(1)); //would print "***".
Console.WriteLine(indent.Redraw(2)); //would print "******".
Console.WriteLine(indent.Redraw(3)); //would print "*********".
You can use the String constructor:
string result = new String('*', 9); // 9 *
If you really want to repeat a string n-times:
string indent = "***";
string result = String.Concat(Enumerable.Repeat(indent, 3)); // 9 *
Yes you are looking for PadLeft() Or, in your case even PadRight() will do the trick :
string indent = "".PadLeft(20, '*'); //repeat * 20 times

Convert int (number) to string with leading zeros? (4 digits) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Number formatting: how to convert 1 to "01", 2 to "02", etc.?
How can I convert int to string using the following scheme?
1 converts to 0001
123 converts to 0123
Of course, the length of the string is dynamic. For this sample, it is:
int length = 4;
How can I convert like it?
Use String.PadLeft like this:
var result = input.ToString().PadLeft(length, '0');
Use the formatting options available to you, use the Decimal format string. It is far more flexible and requires little to no maintenance compared to direct string manipulation.
To get the string representation using at least 4 digits:
int length = 4;
int number = 50;
string asString = number.ToString("D" + length); //"0050"
Use the ToString() method - standard and custom numeric format strings. Have a look at the MSDN article How to: Pad a Number with Leading Zeros.
string text = no.ToString("0000");
val.ToString("".PadLeft(length, '0'))

Categories

Resources