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
Related
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
This question already has answers here:
To get specific part of a string in c# [closed]
(5 answers)
Closed 4 years ago.
I have a string variable called price which contains a range of price pattern as you can see in code. I want to filter it by its first part only.
string price = "9.99 - 12.99";
i already tried like bellow but this gives me wrong output something like ".99" however this is not my target output i am looking for.
string result = price.Substring(1, price.IndexOf("-") - 1);
The output i want like this- "9.99". Now can i filter this part from that string? Thanks in advance
Strings in C# are zero-based, so when you set the starting character as 1, you're in fact starting from the second character. Just use 0 and you should be OK:
string result = price.Substring(0, price.IndexOf("-") - 1);
Note, by the way, that you could use the full " - " delimiter instead of playing around with index arithmetic:
string result = price.Substring(0, price.IndexOf(" - "));
You're close but note that strings are 0 indexed:
string result = price.Substring(0, price.IndexOf("-") - 1);
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]);
This question already has answers here:
Get Second to last character position from string
(10 answers)
Closed 5 years ago.
* I cannot delete this duplicate question because someone has answered it *
I have file names formatted CustomerInfoDaily.12042014.080043 and CustomerInfoDaily.A.12042014.080043 I'm trying to get the base name (CustomerInfoDaily) and the base suffix (.12042014.080043) using substrings. There is no limit to the number of periods however the suffix is always .\d{8}.\d{8}
string fn = "CustomerInfoDaily.A.12042014.080043";
string baseFileName = fn.Substring(0, fn.LastIndexOf(".",fn.Length-1,fn.Length));
string baseSuffix = fn.Substring(fn.LastIndexOf(".", 0, 2));
The problem is that you can say you want the first or last dot but there is no saying that you want the second to the last instance of the dot.
Any help or advice would be greatly appreciated.
Consider using string.Split:
string fn = "CustomerInfoDaily.A.12042014.080043";
var split = fn.Split('.');
var last = split.LastOrDefault();
var secondLast = split.Skip(split.Length - 2).FirstOrDefault();
This question already has answers here:
Format string to a 3 digit number
(9 answers)
Closed 8 years ago.
I want to convert a number to a string but have the number formatted with 10 digits. For example, if the number is 5, the string should be "0000000005". I checked the formatting of strings at:
http://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx
but there isn't any format that lets you specify the number of digits.
Actually the "0" placeholder would work but in reality I need 100 places, so I'm not going to use the "0" placeholder.
You can use the ToString formatting Dn to output leading zeroes:
var d = 5;
var s2 = d.ToString("D2");
var s10 = d.ToString("D10");
Console.WriteLine(s2);
Console.WriteLine(s10);
The output is:
05
0000000005
Normally the D specifier for standard numeric format strings is enough with its precision to format a number with the required number of leading zeros.
But it stops at 99 and if you really need 100 leading zeros you need to resort to the old trusty method of string concatenation and right truncation
int number = 5;
string leadingZero = new string ('0', 100) + number.ToString();
string result = leadingZero.Substring(leadingZero.Length - 100);
This page should help you find the solution you need: http://msdn.microsoft.com/en-us/library/dd260048(v=vs.110).aspx