C#, Concatenate integer and string into var - c#

How to concatenate integer and string into var?
int a; int x=2; int y=7200;
a=x*y;
var B=a+"D"; // How to concatenate this to turn it 14400D
// I need use this in the code that changes the AxisX.LabelStyle.Interval.
// We can not use string concatenation here.
chart1.ChartAreas[0].AxisX.LabelStyle.Interval=B;

.Interval takes a double, could you not just convert the int to a double instead?
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = Convert.toDouble(a);
Interval method
toDouble

By Looking your code. I think you need this code it seems.?
int a; int x = 2; int y = 7200;
a = x * y;
var B = a.ToString() + "D";
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = B;
OR
int a; int x = 2; int y = 7200;
a = x * y;
String aValue = a.ToString() + "D";
var B = aValue;
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = B;
Exactly If your requirement this, then I would suggest first one.

Instead of int a; write double a; and:
chart1.ChartAreas[0].AxisX.LabelStyle.Interval = a;

Related

New to coding, trying to use string variable with readline to change value of another integer variable

I'm trying to use the input from Console.ReadLine() for SpellDCAbilitymod to change the value of the int abilitymod between one of the six integers following abilitymod so that the calculation at the bottom of the code automatically finds the users Spell Save DC depending on the user's input.
This is the relevant section of code I'm having trouble with:
int strmod = (strength - 10) / 2;//formula for Ability Modifiers
int dexmod = (dexterity - 10) / 2;
int conmod = (constitution - 10) / 2;
int intmod = (intelligence - 10) / 2;
int wismod = (wisdom - 10) / 2;
int chamod = (charisma - 10) / 2;
int abilitymod = 0;
int Strength = 1;
int Dexterity = 2;
int Constitution = 3;
int Intelligence = 4;
int Wisdom = 5;
int Charisma = 6;
Console.WriteLine("What is your spellcasting ability?");
string SpellDCAbilitymod = (Console.ReadLine());
if (SpellDCAbilitymod = Charisma)
{
abilitymod = chamod;
}
Console.WriteLine("What is your level?");
int level = Convert.ToInt32(Console.ReadLine());
int u = 0;
int v = 2;
int w = 3;
int x = 4;
int y = 5;
int z = 6;
if (level>0) //Changes value of proficiency based on level input
{
u = v;
}
if (level>4)
{
u = w;
}
if (level>8)
{
u = x;
}
if (level>12)
{
u = y;
}
if (level>16)
{
u = z;
}
int proficiency = u;
Console.WriteLine("Strength:{0}(+{1})", strength, strmod);//Prints Ability Scores and Ability Score Modifiers
Console.WriteLine("Dexterity:{0}(+{1})", dexterity, dexmod);
Console.WriteLine("Constitution:{0}(+{1})", constitution, conmod);
Console.WriteLine("Intelligence:{0}(+{1})", intelligence, intmod);
Console.WriteLine("Wisdom:{0}(+{1})", wisdom, wismod);
Console.WriteLine("Charisma:{0}(+{1})", charisma, chamod);
Console.WriteLine("Proficiency:+{0}", proficiency);
int StuddedLeather = 12;
int AC = StuddedLeather + dexmod;
Console.WriteLine("AC:{0}", AC);
int SpellDC = 8 + proficiency + abilitymod; //Calculates Spell Save DC based on proficiency and abilitymod
Console.WriteLine("You Spellcasting Save DC is: {0}", SpellDC);
Error CS0029 Cannot implicitly convert type 'int' to 'string'
Your mistakes are here:
if (SpellDCAbilitymod = Charisma)
A. You should use == since you're comparing values, not assigning a value.
B. You're comparing a string (SpellDCAbilitymod) to an int (Charisma).
You should either convert SpellDCAbilitymod to an int or convert Charisma to a string.
Here's an example of a fix:
if (SpellDCAbilitymod == Charisma.ToString())
This will compare the string SpellDCAbilitymod to a string convertion of Charisma.
Note that it will not affect the Charisma variable. It will just use the convertion for the if statement.

median c# wrong calculate

I have problem with the calculation of the median when I put 1,2, 3 my median is = 44 i don't know why
double wynik = 0;
string x1 = textBox1.Text;
string[] tab = x1.Split(',');
int n = tab.Length;
Array.Sort(tab);
if (n % 2 == 0)
{
double c = x1[(n / 2) -1];
double v = x1[(n / 2)];
wynik = (c + v) / 2;
}
else
wynik = x1[n / 2];
textBox2.Text = wynik.ToString();
That is because 44 is the ASCII value of ,. And in your string, using your current method now, the median is the comma character , value = 44
To get the median, consider of splitting the string by , and then convert each value to numeric data (like int) and then sort it and simply get the middle value among the sorted data..
double wynik = 0;
string x1 = textBox1.Text;
int[] tab = x1.Split(',').Select(x => Convert.ToInt32(x)).ToArray(); //this is the trick
int n = tab.Length;
Array.Sort(tab);
int median = tab[n/2]; //here is your median
Your problem is that you are calculating with characters instead of number.
So let's say your textBox1.Text is "1,2,3". Then x1[(n/2)-1] would point at the character '1', which has the double value of 48 or something.
You need to parse the strings into int using int.Parse:
int[] tab = x1.Split(',').Select(s => int.Parse(s)).ToArray();
And then use these values instead the string again:
if (n % 2 == 0)
{
double c = tab[(n / 2) -1]; // tab instead of x1!
double v = tab[(n / 2)]; // tab instead of x1!
wynik = (c + v) / 2;
}
else
wynik = tab[n / 2]; // tab instead of x1
static void Main(string[] args)
{
Console.WriteLine("Define Array Size");
int size = Convert.ToInt32(Console.ReadLine());
float reference = 0;
int[] newArray = new int[size];
for (int i = 0; i < newArray.Length; i++)
{
newArray[i] = Convert.ToInt32(Console.ReadLine());
reference = reference + newArray[i];
}
float Median = reference / newArray.Length;
Console.WriteLine("The Median is ="+Median);
}

How to shift all the whole numbers in a double to the right of the point

How to shift all the whole numbers in a double to the right of the point ?
Example i have 5342, i want the function to return 0.5342. I do not know the number of digits in the double, it's randomly generated. Should be fairly easy but i can't find any answers.
private static void Main(string[] args)
{
Console.WriteLine(MyFunction(5127));
Console.WriteLine(MyFunction(1));
Console.WriteLine(MyFunction(51283271));
Console.WriteLine(MyFunction(-512));
Console.WriteLine(MyFunction(0));
}
public static double MyFunction(double myNumber)
{
return Math.Floor(myNumber) / Math.Pow(10, Math.Abs(myNumber).ToString().Length);
}
This sounds like a pretty bizarre task, to be honest, but you could use:
while (Math.Abs(value) >= 1)
{
value = value / 10;
}
That will go into an infinite loop if the input is infinity though - and you may well lose information as you keep dividing. The latter point is important - if what you're really interested in is the decimal representation, you should consider using decimal instead of double.
You could potentially use a mixture of Math.Log and Math.Pow to do it, but the above is probably what I'd start with.
This will get you most of the way there
public static string test()
{
double dub = 5432;
string dubTxt = dub.ToString();
string text = "0.";
string test = string.Concat(text + dubTxt);
if (1 == 1)
{
MessageBox.Show(test);
return test;
}
}
You will have to develop more if statements to handle the negative numbers.
public static string test()
{
double dub = 5432;
string dubTxt = dub.ToString();
string text = "0.";
string test = string.Concat(text + dubTxt);
if (dub < 0)
{
//Do this code instead
}
}
Good Luck. Please bump me if you choose it!! I need the cred so I can do other junk. :-D
Just divide by 10 until the number is less than 1.
public static double SomeMethod(double n)
{
double d = n;
bool isNegative = (d < 0);
if(isNegative)
d = d * -1;
while(d >= 1)
d = d/10;
if(isNegative)
d = d * -1;
return d;
}
Alternative (and more precise) option:
public static double SomeMethod2(double n)
{
double d = n;
bool isNegative = (d < 0);
if(isNegative)
d = d * -1;
int numberOfDigits = ((int)d).ToString().Length;
int divisor = 1;
for(int i = 0; i < numberOfDigits; i++)
divisor = divisor * 10;
d = d/divisor;
if(isNegative)
d = d * -1;
return d;
}

Problem with return in C#

Because of asking my last question, I've changed my code to use return. I always have this problem with return: I know I should use return (where I comment in code below) but I dont know how should I define it to work? Thanks for your help.
public double bigzarb(int u, int v)
{
double n;
int x=0;
int y;
int w=0;
int z;
string[] i = textBox7.Text.Split(',');
int[] nums = new int[i.Length];
for (int counter = 0; counter < i.Length; counter++)
{
nums[counter] = Convert.ToInt32(i[counter]);
}
u = nums[0];
double firstdigits =Math.Floor(Math.Log10(u) + 1);
v = nums[1];
double seconddigits = Math.Floor(Math.Log10(v) + 1);
if (firstdigits >= seconddigits)
{
n = firstdigits;
}
else
{
n = seconddigits;
}
if (u == 0 || v == 0)
{
MessageBox.Show("the Multiply is 0");
}
string threshold = textBox9.Text;
int intthreshold = Convert.ToInt32(threshold);
int intn = Convert.ToInt32(n);
if (intn <= intthreshold)
{
double uv = u * v;
string struv = uv.ToString();
MessageBox.Show(struv);
///i know here should be a return but i dont know how to define it to work
}
else
{
int m = Convert.ToInt32(Math.Floor(n / 2));
x = u % 10 ^ m;
y = u / 10 ^ m;
w = v % 10 ^ m;
z = v / 10 ^ m;
return bigzarb(x, w) *Math.Pow(10,m) +(bigzarb(x,w)+bigzarb(w,y))*Math.Pow(10,m) +bigzarb(y,z);
}
}
arash, your problem isn't with that return, your problem is bigzarb() is declared as void which means it has no returning value yet you use it in your last line as bigzarb(x,w) * .... which will give you an error. Also, since you declared your bigzarb() as void, you cant return a value in it. Also ^ doesn't mean power of in .net, you should use Math.Power instead.
Edit: You should change your method from void bigzarb() to double bigzarb() and replace ^ with Math.Power and retry to see if yit works.
Last edit: Change your method return type to double from int and change the last line to:
return bigzarb(x, w) * Math.Pow(Convert.ToDouble(10), Convert.ToDouble(m)) + (bigzarb(x, w) + bigzarb(w, y)) * Math.Pow(Convert.ToDouble(10), Convert.ToDouble(m)) + bigzarb(y, z);
If your method is defined to "return void", you can't return some value.
So change that "void" into "int" or "double", whatever type of value you want to return.
The next question is what value do you want to return in that spot? Return that.
By the way: if you really don't want to return a value (ever!) then that "void" is correct and you should only use "return" without value (or let the method run until the last line of the method).
my tip to you is to structure your code, extract some of the code to separate functions with meaningful names and also rename your variables to sometging meaningful. That would make it easier to read, understand and you get a better "flow" in your code. Also, you should complement your text with an actual question. Try to insert an extra return statement in the if code block, it is perfectly fine to have multiple return statements.
if(logic check)
{
return something;
}
else
{
return something else;
}
You need to have return either in both statements or one return after the if-else block, otherwise you'll get somekind of compiler error saying that not all code paths returns a result or something similar.
You have only one return and it's a recursive call (calling itself) --> stack overflow! You need to have another return somewhere without a recursive call
public int bigzarb(int u, int v)
{
double n;
int x = 0;
int y;
int w = 0;
int z;
string[] i = textBox1.Text.Split(',');
int[] nums = new int[i.Length];
for (int counter = 0; counter < i.Length; counter++)
{
nums[counter] = Convert.ToInt32(i[counter]);
}
u = nums[0];
double firstdigits = Math.Floor(Math.Log10(u) + 1);
v = nums[1];
double seconddigits = Math.Floor(Math.Log10(v) + 1);
if (firstdigits >= seconddigits)
{
n = firstdigits;
}
else
{
n = seconddigits;
}
if (u == 0 || v == 0)
{
MessageBox.Show("the Multiply is 0");
}
//string threshold = textBox9.Text;
int intthreshold = Convert.ToInt32(textBox9.Text);//Edited by me
int intn = Convert.ToInt32(n);
if (intn <= intthreshold)
{
double uv = u * v;
string struv = uv.ToString();
MessageBox.Show(struv);
///i know i should use return here but how can i implement that to work?
}
else
{
int m = Convert.ToInt32(Math.Floor(n / 2));
x = u % 10 ^ m;
y = u / 10 ^ m;
w = v % 10 ^ m;
z = v / 10 ^ m;
return bigzarb(x, w) * (10 ^ m) + (bigzarb(x, w) + bigzarb(w, y)) * 10 ^ m + bigzarb(y, z);
}
return 0;
}
Its a confusing one.since your function contains Void as return type
Declare the integer variable for returning the values
Ex:
public int func()
{
int l_nData = 0;
if(condition)
{
l_nData = 1;
return l_nData;
}
else
{
l_nData = 2;
return l_nData;
}
return l_nData;
}

Cannot implicitly convert type 'double' to 'long'

In this code i got the above error in lines i commented.
public double bigzarb(long u, long v)
{
double n;
long x;
long y;
long w;
long z;
string[] i = textBox7.Text.Split(',');
long[] nums = new long[i.Length];
for (int counter = 0; counter < i.Length; counter++)
{
nums[counter] = Convert.ToInt32(i[counter]);
}
u = nums[0];
int firstdigits = Convert.ToInt32(Math.Floor(Math.Log10(u) + 1));
v = nums[1];
int seconddigits = Convert.ToInt32(Math.Floor(Math.Log10(v) + 1));
if (firstdigits >= seconddigits)
{
n = firstdigits;
}
else
{
n = seconddigits;
}
if (u == 0 || v == 0)
{
MessageBox.Show("the Multiply is 0");
}
int intn = Convert.ToInt32(n);
if (intn <= 3)
{
long uv = u * v;
string struv = uv.ToString();
MessageBox.Show(struv);
return uv;
}
else
{
int m =Convert.ToInt32(Math.Floor(n / 2));
x = u % Math.Pow(10, m); // here
y = u / Math.Pow(10, m); // here
w = v % Math.Pow(10, m); // here
z = v / Math.Pow(10, m); // here
long result = bigzarb(x, w) * Math.Pow(10, m) + (bigzarb(x, w) + bigzarb(w, y)) * Math.Pow(10, m) + bigzarb(y, z);///here
textBox1.Text = result.ToString();
return result;
}
}
Whats is the problem? Thanks!
The Math.Pow method returns a double, not a long so you will need to change your code to account for this:
x = (long)(u % Math.Pow(10, m));
This code will cast the double result from Math.Pow and assign that value to x. Keep in mind that you will lose all the precision providided by decimal (which is a floating-point type and can represent decimal values). Casting to long will truncate everything after the decimal point.
Change types
long x;
long y;
long w;
long z;
to
double x;
double y;
double w;
double z;
Or make use of
Convert.ToInt64
Math.Pow returns a double.
the Right Hand Side (RHS) of % can only be an integer type.
you need
x = u % (long)Math.Pow(10, m);///<----here
y = u / (long)Math.Pow(10, m);///here
w = v % (long)Math.Pow(10, m);///here
z = v / (long)Math.Pow(10, m);///here
Additionally, You have the possibility of dividing by zero and destroying the universe.
Math.Pow returns a double. You could explicitly cast to long, for example
x = u % (long)Math.Pow(10, m);
although that is likely not the correct solution. Are you certain that the results that you are after can be properly expressed as a double? If not then change the variables to be declared as doubles rather than longs.
You cant' cast implicitly double to long, use (long) cast or change type of variable declaration to double.
Also you can use this:
Convert.ToInt64( u % Math.Pow(10, m) )
Source here

Categories

Resources