How to append a string in C# dynamically? [closed] - c#

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Thank you in Advance:
I have a parameter which gives the row_count after loading the data.
i need to insert that value into a file, which i am able to do it.
But I need the format in this manner:
if the row_count is 150 then
I am getting only Param name='INPUT_NUM_RECS value=150/
But I need the output as
Param name='INPUT_NUM_RECS value=000000150/
Thank you.

Assuming that 150 is an int variable name num.
int num = 15;
string output = $"Param name='INPUT_NUM_RECS value={num:D9}"
Here using D9 we will place the int value with leading zeroes if the number of digits in num is less than 9.
Number of leading zeroes = 9 - digits in num
If number of digits in num is more than 9, num will not be truncated

Related

What is the correct way of writing: else if (9<result<16) in C#? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
the syntax:
(9<result<16)
is appearing to be invalid when I'm using it in my C# code. I want the else if statement to consider the condition: when the sum of two integers is greater than 9 and less than 16, and return the line :"That's a poor grade" as a result. But the problem is that writing the condition as (9
Screen shot of code to give you context
Edit: ignore the following as I'm writing all this to make the edit more substantive, so that my post can be corrected
You can convert to
else if (9 < result && result < 16)
Also, you need return int value in Add method as
public static int Add (int num01, int num02){
//Console if need
return num01 + num02;
}

calculate multiple values type to string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
i have two textboxes 1 called 'mtb_NETPAIE02' and 2 called 'mtb_TAXE02'
the format in the two textboxes is currency/money (double) i want to fix the error on this Code :
if (double.Parse(mtb_NETPAIE02.Text) >= 100001 )
{
mtb_TAXE02.Text = (double.Parse(mtb_NETPAIE02.Text) / 5000 * double.Parse("12")
+ double.Parse("18").ToString("N2")).ToString();
}
12 (12,00) and 18 (18,00) are money but 5000 is int .
how to make this right ?
First of all, you should not be using double if you're working with money; you should be using decimal.
Secondly, you should use the literals for decimal, i.e. with the M suffix.
Finally, you are attempting to add the result of decimal.Parse("18") to the other numeric values, but you are converting it to a string first via ToString("N2"). You should move the latter outside your parens to convert the whole result from the calculation into a string:
if (decimal.Parse(mtb_NETPAIE02.Text) >= 100001M )
{
// notice the `M` in 5000M
mtb_TAXE02.Text = (decimal.Parse(mtb_NETPAIE02.Text) /
5000M * decimal.Parse("12") + decimal.Parse("18")).ToString("N2");
}

How to count the number of different digits in a number? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is a code that counts the number of digits in a number recursively. What can I add to this code to count how many different digits are in a number? Or maybe there is some other way?
int numberOfDigits(int n)
{
if(n==0)
return 0;
else
return numberOfDigits(n/10)+1;
}
Use sets!
static int NumberOfDigits(int a) {
return new HashSet<char>(Math.Abs(a).ToString()).Count;
}
We make a into a string and then turn the string into a set of characters. Since sets cannot contain duplicate values, the count of the set is the number of distinct digits.

C# Calculating with strings? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I need help with this algorythm :
int num = this.UserID * 786;
this.ValidCode = num * 17;
num = this.ValidCode / 12;
this.ValidCode = num + 1991;
I found this code written in vb.net and converted it online to C# as I want to use this for some kind of licensing system.
It wont work because VS tells me i cant calculate with strings or cant convert int to string.
UserID is a string getting from a textbox .
I need a way to get it working to work with UserID variable got from textbox1 and textbox 2 should display the final result
You can't convert string to int implicitly, you need to use built-in function for that.
int num = Convert.ToInt32(this.UserID) * 786;
int num = Convert.ToInt32(this.UserID) * 786;
this.ValidCode = num.ToString();

How do I make a string only X char and specific? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know if you got me what I'm trying to explain... (English is not my main language).
Ok here, I got this string 590CBC145FA and this one F6EC5CA9A and the only part that I want, no matter how long is the string, is the last eight char like this: CBC145FA 6EC5CA9A. The 590 and F are obsolete to me, like: !S!ZV)+D_?CEFZEZAF = CEFZ!Z#F.
I tried to use serial.Substring(3); only work for the first one: 590CBC145FA = CBC145FA
If I try to use serial.Substring(1); it work for F6EC5CA9A = 6EC5CA9A
But what happen if I get a string very long and I don't know how much the obsolete part is... I can't use serial.Substring(X); because I don't, like I said, how long is the obsolete part.
I only want to get the eight last char of any serial no matter how long is.
You want to get the eight last char of any serial no matter how long is.
string test = "F6EC5CA9A";
string result = test;
if (test.Length >= 8)
result = test.Substring(test.Length-8);
You just need to discover the index of the 8th character before the end of the string. And this could be easily calculated using the Length property of every string.
Of course you need to be sure that your string is at least 8 characters. You don't say what do you want in case this length is less than 8, so I assume that you want the original input back

Categories