views:

110

answers:

2

I want to calculate the percentage. But the compiler is giving an error that the input string is not in a correct format. Can some one elaborate what i am missing here?

private double per()
{
  double a = Convert.ToDouble(tbEnglish.Text+tbUrdu.Text+tbPhysics.Text+tbChemistry.Text+tbMaths.Text);
  double d = 500;
  double lblResult = (a / d)*100;
  return lblResult;
 }
+4  A: 

You're concatenating the strings and then trying to convert that one result into a double. So for results of 75.6, 92.1, 56.3 78.2 and 72.3 you'd end up trying to parse "75.692.156.378.272.3".

Parse each value and then sum them.

However, I would strongly recommend that you use decimal for this instead of double. You should also consider using TryParse instead of Parse so that you can handle user input errors gracefully. Here's the solution sticking with Parse:

public decimal AveragePercentage()
{
    decimal sum = decimal.Parse(tbEnglish.Text) +
                  decimal.Parse(tbUrdu.Text) +
                  decimal.Parse(tbPhysics.Text) +
                  decimal.Parse(tbChemistry.Text) +
                  decimal.Parse(tbMaths.Text);
    return sum / 5m;
}

Out of interest, in your original code why are you dividing by 500 and then multiplying by 100? Why not just divide by 5 (as mine does now that I've noticed what was going on)?

As a side note, it's very important to differentiate between compile-time errors and execution-time errors. It wasn't the compiler saying that the input string wasn't in the correct format - it was the Convert.ToDouble method, at execution time. In this case it was relatively obvious, but in other situations we could have been chasing our tails for a while trying to find a compile-time problem when it was actually failing at execution time.

Jon Skeet
I feel like an idiot for asking, but what is the "500m" syntax? What's the 'm' do?
Sapph
@Sapph: It indicates a numeric literal of the `decimal` type. (i.e. it's the decimal equivalent of `d` for double or `f` for float)
Jon Skeet
Very helpful to know, thank you!
Sapph
this AveragePercentage is a built in function huh?
Abid
@Sapph: You can do `var x = 5m;` and place your mouse over var to see the datatype it becomes.
Yuriy Faktorovich
@jon Skeet : i`m not getting this Averagepercentage function in my compiler! :/
Abid
@Abid: `AveragePercentage` is not a built-in function at all. Jon Skeet just showed you how to write it! :) Not to put words in his mouth, but he may have been suggesting that "AveragePercentage" is a clearer name for your "per" function.
Sapph
ohh.. :) such a foolish of me.. btw.. the function by JON also gives the same exception... i don`t know why..
Abid
@abid: that suggests there's something wrong with your data. What are the values in your textboxes? (This is why you should use TryParse btw - you can validate the user input without an exception.)
Jon Skeet
i`m giving value like 34.6 65.4 63.8
Abid
@Abid: Which line is throwing the exception, and what's the value for the corresponding textbox? Also, what's the default culture for your box?
Jon Skeet
A: 

I don't have Visual Studio available to me here on my Linux box, but I think you're better off with code like this.

private double per()
{
   double a = Convert.ToDouble(tbEnglish.Text);
   a += Convert.ToDouble(tbPhysics.Text);
   a += Convert.ToDouble(tbChemistry.Text);
   a += Convert.ToDouble(tbMaths.Text);
   double d = 500;
   double lblResult = (a / d)*100;
   return lblResult;
}

In your example, you end up building a string that will look like: "75.692.156.372.3", which cannot be parsed into a double.

You need to convert all the TextBox.Text values into Decimals before using the + operator.

Dominic Barnes
@Dominic your snippet aint working too.. :/ i guess there`s a problem with my code...
Abid