can someone show me how i can split value of double like this:-
654782.690
Can i split the value into this =
int i[0] = 65
int i[1] = 4
int i[2] = 7
int i[3] = 8
double i[4] = 2.690
Appreciate for the answer. Thank you in advance.
EDITED***
I have the UTM coordinate value in my xml. I want to make the chart and the value for the coordinate is fall into the grid. Lets say i have the zone area of x1 (578462.254), y1 (648523.124) & x2 (578481.452), y2 (648474.156). In my xml, i have a datapoint which fall under this coordinate. What i would like to do is to make the chart grid with the first two digit is become the zone area min and max(x,y).
I hope my clarification can explain what i want to do with those data. Thank you very much.
I got the answer here:-
decimal dValue = 654782.690;
string sValue = dValue.ToString();
decimal[] dArray = new decimal[5];
dArray[0] = Convert.ToDecimal(sValue.Substring(0, 2));
dArray[1] = Convert.ToDecimal(sValue.Substring(2, 1));
dArray[2] = Convert.ToDecimal(sValue.Substring(3, 1));
dArray[3] = Convert.ToDecimal(sValue.Substring(4, 1));
dArray[4] = Convert.ToDecimal(sValue.Substring(5));
Its worked now!!! Thank you for your support to solve my problem. :)
Use decimal instead of double if you are not storing scientific value.
decimal dValue = 654782.690M;
string sValue = dValue.ToString();
deciaml[] dArray = new decimal[5];
dArray[0] = Convert.ToDecimal(sValue.Substring(0,2));
dArray[1] = Convert.ToDecimal(sValue.Substring(2,1));
dArray[2] = Convert.ToDecimal(sValue.Substring(3,1));
dArray[3] = Convert.ToDecimal(sValue.Substring(4,1));
dArray[4] = Convert.ToDecimal(sValue.Substring(5));
To use the value of array index of 0 to 3 use Convert.ToInt32() to convert in Int datatype.
int iValue = Convert.ToInt32(dArray[0]);
Above example is not compiled it may give error. Take this example as a logic.
Related
I am pretty new on the programming field. I started learning C# and I have a problem.
I need to find the average of the given numbers from textboxes.
int wage1 = int.Parse(textBox_wage1.Text);
int wage2 = int.Parse(textBox_wage2.Text);
int wage3 = int.Parse(textBox_wage3.Text);
int wage4 = int.Parse(textBox_wage4.Text);
int wage5 = int.Parse(textBox_wage5.Text);
int wage6 = int.Parse(textBox_wage6.Text);
int wage7 = int.Parse(textBox_wage7.Text);
var average = new int[] { wage1, wage2, wage3, wage4, wage5, wage6, wage7 };
double AverageBruto = Queryable.Average(average.AsQueryable());
lbl_AverageBruto.Text = "Average: " + AverageBruto;`
This works perfectly fine if the user has filled all 7 textboxes, but if they didn't fill at least one of them, it causes an error. can someone help me figure out how I can find the average if they fill only 3 or 4 of the textboxes? Thank you.
You likely don't want to be using an array for this, because if null values are represented by zeros then the average will be off. The wage ints should probably be in an array (The textBox_wages as well but it's not necessary for this example) so you can iterate through them, check for null values, and only add those that aren't null to a list you'll take the average from. Here's one way you could approach it:
int[] wages = new int[8];
int wages[0] = int.Parse(textBox_wage1.Text);
int wages[1] = int.Parse(textBox_wage2.Text);
int wages[2] = int.Parse(textBox_wage3.Text);
int wages[3] = int.Parse(textBox_wage4.Text);
int wages[5] = int.Parse(textBox_wage5.Text);
int wages[6] = int.Parse(textBox_wage6.Text);
int wages[7] = int.Parse(textBox_wage7.Text);
var AverageBruto = new List<int>();
foreach (int x in wages) if (x != null) AverageBruto.Add(i);
AverageBruto = AverageBruto.average();
lbl_AverageBruto.Text = "Average: " + AverageBruto;
You'll have to use System.Linq for this method though.
Let's organize the TextBoxes and then query with a help of Linq:
double AverageBruto = new TextBox[] {
textBox_wage1,
textBox_wage2,
textBox_wage3,
textBox_wage4,
textBox_wage5,
textBox_wage6,
textBox_wage7,
}
.Where(box => !string.IsNullOrWhiteSpace(box.Text))
.Average(box => int.Parse(box.Text));
lbl_AverageBruto.Text = $"Average: {AverageBruto}";
Here, with a help of Where we choose only TextBoxes which have some Text
I am working in c# and i do not want thing like this:
value = 1.658598554646 to value= 2
or
value = 1.2256485526 to value =1
I want to do something like :
value = 1.658598554646 to value = 1.7 //see upto one intger after point(.)
value = 1.2256485526 to value = 1.2
How to do that in c# ? Is there any way to achieve this ?
one decimal you can use Math.Round, and set the number of decimal points you round it to,
decimal value = 1.658598554646;
OneDec = Math.Round(value , 1);
How use Math.Round
var result = Math.Round(value, 1);
you can use
decimal DEBITAMT = Math.Round(1.658598554646, 1);
protected void btnView_Click(object sender, EventArgs e)
{
int StudentAdmissionNo = Convert.ToInt32(txtAdmissionNo.Text);
string StudentName = txtStudentName.Text;
DateTime DateOfBirth = Convert.ToDateTime(txtDOB.Text);
string Gender = txtGender.Text;
string Standard = txtstandard.Text;
string Section = txtSection.Text;
int Telugu = Convert.ToInt32(txtTelugu.Text);
int English = Convert.ToInt32(txtEnglish.Text);
int Mathematics = Convert.ToInt32(txtMathematics.Text);
//int Total = Convert.ToInt32(lblTot.Text);
//double Average = Convert.ToDouble(lblAve.Text);
string Result = lblRes.Text;
lblTot.Text = (txtTelugu.Text + txtEnglish.Text + txtMathematics.Text); # Total in coming if i enter the 45 65 65 = its coming 456565 like i did wrong?
lblAve.Text = ((lblTot.Text) / 3.00); //This also gives an error
}
In this code I have an error coming from lblTot = to add marks and it coming like same to printing that numbers what I entered and average also not working.
You are concatenating the Text. When you use + with text/string it acts as a concatenation operator.
You need to convert text to Integer for arithmatic operations first.
Use Convert.ToInt32(input);
Replacing your code with the below lines should fix it.
lblTot.Text = (Convert.ToInt32(txtTelugu.Text) + Convert.ToInt32(txtEnglish.Text) + Convert.ToInt32(txtMathematics.Text)).ToString();
lblAve.Text = ((Convert.ToInt32(lblTot.Text)) / 3.00).ToString();
Update
I just noticed that you have already assigned the required values to integers here:
int Telugu = Convert.ToInt32(txtTelugu.Text);
int English = Convert.ToInt32(txtEnglish.Text);
int Mathematics = Convert.ToInt32(txtMathematics.Text);
Which is correct, you just needed to add these variables, like:
int total = Telugu + English + Mathematics;
lblTot.Text = total.ToString();
And then, for average just use the total variable that we just assigned the value to:
lblAve.Text = Convert.ToString(Convert.ToDouble(total / 3.00)); // convert the average to double as you may get values with decimal point. And then convert it back to string in order to assign it to a Label control.
If my assumption of what you're trying to do is correct your code should look like this.
decimal total = Teluga + English + Mathematics;
decimal average = (Teluga + English + Mathematics) / 3.00;
lblTot.Text = total.ToString();
lblAve.Text = average.ToString();
You are trying to use strings to perform mathematical operations and that won't work.
Also, typically
int x = Int32.TryParse(txtEnglish.Text)
is a better way to turn string into integers
The root of both your problems is that a textbox.Text property is a string.
In your first problem, you are concatenating strings, which is why it's giving you you 456565.
In your second problem, you are trying to perform arithmetic on a string and a number.
Cast your strings as numbers by declaring an Int32 variable placeholder and using the TryParse method on your string, like so:
Int32 txtTeluguResult;
Int32.TryParse(txtTelugu.Text, txtTeluguResult);
Also note that TryParse will put 0 in for your result variable if it cannot successfully parse the text input. You may wish to use Convert.ToInt32() instead, which throws a FormatException if it fails, depending on what behavior you want.
lblTot.Text = (txtTelugu.Text + txtEnglish.Text + txtMathematics.Text); # Total in coming if i enter the 45 65 65 = its coming 456565 like i did wrong?
lblAve.Text = ((lblTot.Text) / 3.00); this also giving error?
you are using string variables not int or real or decimal
do something like
lblTot.Text = (((int)txtTelugu.Text + (int)txtEnglish.Text + (int)txtMathematics.Text)).ToString();
lblAve.Text = (((int)(lblTot.Text) / 3.00)).ToString(); this also giving error?
I've figured out how to compare 2 text boxes and count how many words are the same in each one and how many are different but I was wondering instead of showing exactly how many are the same and how many are different could I calculate the percentage of difference between them?
For those who I confused, if i wrote this entire message again under this post and changed a few words and maybe removed some I could get feedback from the program telling me the difference is 14% or so...
You can get words by using string.Split:
String[] words1 = Textbox1.Text.Split();
String[] words2 = Textbox2.Text.Split();
You can use Linq to compare both:
var sameWordsInBoth = words1.Intersect(words2);
int count1 = sameWordsInBoth.Count();
var inTxt1ButNotInTxt2 = words1.Except(words2);
int count2 = inTxt1ButNotInTxt2.Count();
var inTxt2ButNotInTxt1 = words2.Except(words1);
int count3 = inTxt2ButNotInTxt1.Count();
To get the percentage you just have to count the total words(f.e. words1.Length) and compare that to the other counts.
For example:
double sameWordPercentage = (1.0 * count1 / words1.Length) * 100;
Since you said you already have the amount of unique words, and dont need explanation on how to get them:
double UniqueWordCount = 71;
double TotalWordCount = 231;
double Percentile = (UniqueWordCount/TotalWordCount)*100;
Answer : 30.7359.... %
If you want to round it
Math.Round(Percentile,2)
Answer : 30.74 %
How to get the first two numbers of an integer and the last two in c#.
For example:
int complete = 1935;
how to make,
int firtTwo = 19;
and
int secondTwo = 35;
Please help.
Is it always a four-digit positive value? If so, I'd go for:
int hundreds = complete / 100;
int tensAndUnits = complete % 100;
If it's any arbitrary number, so you'd want "935" to be "93" and "35" you might as well use string operations:
string text = complete.ToString();
int first = int.Parse(text.Substring(0, 2));
int last = int.Parse(text.Substring(text.Length - 2, 2));
(Note that this will blow up for single-digit values...)
It would help if you could say what this is meant to be for though.