C# Calculating with strings? [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 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();

Related

My space counter doesn't work. How do I fix it? [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
I'm trying to make a word counter in C# but it doesn't work. I don't understand the problem. The code is:
int spaceCount = 0;
Console.WriteLine("Tell me a sentens: ");
string sentens =Convert.ToString(Console.ReadLine());
spaceCount = Convert.ToInt32(sentens.Split(' '));
Console.WriteLine(spaceCount);
Console.ReadKey();
This code works except for the
spaceCount = Convert.ToInt32(sentens.Split(' '));
Line and I don't understand why.
Try this,
int spaceCount = 0;
Console.WriteLine("Tell me a sentence: ");
string setences = Console.ReadLine();
spaceCount = sentences.Split(' ').Length;
Console.WriteLine(spaceCount);
Console.ReadKey();
There is no need to do so much converting, especially converting the split to an integer. It returns an array, which you can just call .Length on to retreieve the amount of items in the array.

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;
}

How to append a string in C# dynamically? [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 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

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.

Categories