Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn null to 0

Tags:

c#

.net

I built a little program that calculates the average of 15 numbers or less. There are 15 text-boxes, each one's default value is '0'. The program knows to get the sum of all typed numbers and divide it by numbers of text boxes that don't return '0'. But if the user deletes in mistake one of the '0'os in one of the text boxs.. run-time error.

Originally I solved this problam by writing this "if statement" 15 times(one for each text-box):

 if (t1.Text == "") { tr1 = 0; }
 else
 {
     tr1 = Double.Parse(t1.Text);
 }

this code checks if there isn't a thing in text box(for example, named t1), if true, the program is giving the double 'tr1'(don't confuse with 't1'), the value of '0', if false, the code gives the double 'tr1' the text of 't1'.

i had to write this 'if' 15 times. i wanted to know if i can write the same code with arrays and a for loop, and how?

here is the whole code (sorry for var names are not similar to var's use.):

 private void goyouidiot_Click(object sender, EventArgs e)
 {
     double tr1;
     double tr2;
     double tr3;
     double tr4;
     double tr5;
     double tr6;
     double tr7;
     double tr8;
     double tr9;
     double tr10;
     double tr11;
     double tr12;
     double tr13;
     double tr14;
     double tr15;
     if (t1.Text == "") { tr1 = 0; }
     else
     {
         tr1 = Double.Parse(t1.Text);
     }
     if (t2.Text == "") { tr2 = 0; }
     else
     {
         tr2 = Double.Parse(t2.Text);
     }

     if (t3.Text == "") { tr3 = 0; }
     else
     {
         tr3 = Double.Parse(t3.Text);
     }


     if (t4.Text == "") { tr4 = 0; }
     else
     {
         tr4 = Double.Parse(t4.Text);
     }


     if (t5.Text == "") { tr5 = 0; }
     else
     {
         tr5 = Double.Parse(t5.Text);
     }

     if (t6.Text == "") { tr6 = 0; }
     else
     {
         tr6 = Double.Parse(t6.Text);
     }


     if (t7.Text == "") { tr7 = 0; }
     else
     {
         tr7 = Double.Parse(t7.Text);
     }


     if (t8.Text == "") { tr8 = 0; }
     else
     {
         tr8 = Double.Parse(t8.Text);
     }

     if (t9.Text == "") { tr9 = 0; }
     else
     {
         tr9 = Double.Parse(t9.Text);
     }


     if (t10.Text == "") { tr10 = 0; }
     else
     {
         tr10 = Double.Parse(t10.Text);
     }


     if (t11.Text == "") { tr11 = 0; }
     else
     {
         tr11 = Double.Parse(t11.Text);
     }


     if (t12.Text == "") { tr12 = 0; }
     else
     {
         tr12 = Double.Parse(t12.Text);
     }

     if (t13.Text == "") { tr13 = 0; }
     else
     {
         tr13 = Double.Parse(t13.Text);
     }


     if (t14.Text == "") { tr14 = 0; }
     else
     {
         tr14 = Double.Parse(t14.Text);
     }


     if (t15.Text == "") { tr15 = 0; }
     else
     {
         tr15 = Double.Parse(t15.Text);
     }
     double[] sch = { tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9, tr10, tr11, tr12, tr13, tr14, tr15 };
     double total = 0;
     double sorf = 0;
     for (int i = 0; i != 14; i++)
     {

         sorf = sorf + sch[i];

         if (sch[i] > 0)
         { total++; }

     }

     double totalic = sorf / total;
     string glass = totalic.ToString();
     result.Text = ("your score: " + glass);
}
like image 372
Gilad Naaman Avatar asked Jul 12 '10 06:07

Gilad Naaman


People also ask

How do I convert NULL to zero in Excel?

Alternatively, you can click the Home tab in the Ribbon and then select Go to Special from the Find & Select drop-down menu. Select Blanks in the Go To Special dialog box and click OK. Excel will select all of the blank cells within the range. Type the value you want to enter in the blanks (such as 0, – or text).

How do you make a NULL zero in SQL?

Use IFNULL or COALESCE() function in order to convert MySQL NULL to 0. Insert some records in the table using insert command. Display all records from the table using select statement.

Which function can we use to convert NULL values to zero?

If the value of the control is Null, the procedure uses the Nz function to convert a Null value to a zero-length string.

How do you convert NULL values?

The ISNULL Function is a built-in function to replace nulls with specified replacement values. To use this function, all you need to do is pass the column name in the first parameter and in the second parameter pass the value with which you want to replace the null value.


2 Answers

Double.TryParse(t1.Text.Trim(), out tr1);

will set tr1 to the text box's numeric value, or 0.0 if it failed to convert it for some reason. It'll also return true if the conversion succeeded or false if it failed, but you don't care about the return value if the default is 0.0.

Added bonus: it won't throw an exception if someone decides to put "This is not a number." into a text box. It'll just see the value as 0.

To do this in an array...

TextBox t[] = { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15 };
double tr[] = new double[t.Length];

for (int i = 0; i < t.Length; ++i)
{
    Double.TryParse(t[i].Text.Trim(), out tr[i]);
}

UPDATE:

Note, it's perfectly reasonable to expect to be able to compute an average of numbers that includes 0. In order to do this:

TextBox t[] = { t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15 };
double tr[] = new double[t.Length];
int valid_count = 0;

for (int i = 0; i < t.Length; ++i)
{
    if (Double.TryParse(t[i].Text.Trim(), out tr[i])) ++valid_count;
}

Set your TextBoxes' default values to blank (""), and then you'll know how many were legitimately 0's entered by the user and how many were blank. Divide the sum by valid_count to get an accurate average. (But be sure valid_count > 0, or you'll likely get a divide-by-zero exception.)

like image 107
cHao Avatar answered Oct 07 '22 18:10

cHao


Sure, make a double tr[15] and a corresponding array of text fields.

Then just use:

for (int i = 0; i < 15; i++) {
    if (t[i].Text == "") { 
        tr[i] = 0;
    } else {
        tr[i] = Double.Parse(t[i].Text);
    }
}
like image 40
Borealid Avatar answered Oct 07 '22 18:10

Borealid