How can I find the percentage from thhe value of a label control, and display it in another label? For instance, I have 4 Label controls, each label have values. I want to diplay the percentage for each label value in a label. e.g. These are the Labels with thier values. I want to show the perecentage value of label1.Text Label1.Text = 6 Label2.Text = 10 Label3.Text = 8 Label4.Text = 3 LaabelTotal.Text = 27 So I tried doing this, but it did not work. Please how can I achieve this?
HTML
<div>
<asp:Label runat="server" ID="Label1" Text="6"></asp:Label>
<asp:Label runat="server" ID="Label2" Text="10"></asp:Label>
<asp:Label runat="server" ID="Label3" Text="8"></asp:Label>
<asp:Label runat="server" ID="Label4" Text="3"></asp:Label>
<asp:Label runat="server" ID="LabelTotal" Text="27"></asp:Label>
</div>
Pecentage (%) = <label runat="server" id="LabelPercentage" style="float: right;"></label>
protected void Page_Load(object sender, EventArgs e)
{
TotalCounts();
CalculatePercentage();
//This is where the Total label value is set
if (LabelTotal != null)
{
LabelTotal.Text = (int.Parse(!string.IsNullOrEmpty(Label1.Text) ? Label1.Text : "0") + int.Parse(!string.IsNullOrEmpty(Label2.Text) ? Label2.Text : "0") + int.Parse(!string.IsNullOrEmpty(Label3.Text) ? Label3.Text : "0") + int.Parse(!string.IsNullOrEmpty(ILabel4.Text) ? Label4.Text : "0")).ToString("###,##0");
}
else
{
LabelTotal.Text = "0";
}
}
//This is where the label values are set
private void TotalCounts()
{
Label1.Text = (int.Parse(!string.IsNullOrEmpty(lblcert.Text) ? lblcert.Text : "0") + int.Parse(!string.IsNullOrEmpty(upldlbl.Text) ? upldlbl.Text : "0")).ToString("###,##0");
Label2.Text = (int.Parse(!string.IsNullOrEmpty(labelinv.Text) ? labelinv.Text : "0") + int.Parse(!string.IsNullOrEmpty(lbluld.Text) ? lbluld.Text : "0")).ToString("###,##0");
Label3.Text = (int.Parse(!string.IsNullOrEmpty(recptlbl.Text) ? recptlbl.Text : "0") + int.Parse(!string.IsNullOrEmpty(lblupld.Text) ? lblupld.Text : "0")).ToString("###,##0");
Label4.Text = (int.Parse(!string.IsNullOrEmpty(Label2.Text) ? Label2.Text : "0") + int.Parse(!string.IsNullOrEmpty(idlabel.Text) ? idlabel.Text : "0")).ToString("###,##0");
PermitTotal.Text = (int.Parse(!string.IsNullOrEmpty(Label1.Text) ? Label1.Text : "0") + int.Parse(!string.IsNullOrEmpty(Label3.Text) ? Label3.Text : "0")).ToString("###,##0");
}
private void CalculatePercentage()
{
// Convert the label text to integers
int label1Value = Convert.ToInt32(Label1.Text);
int totalValue = Convert.ToInt32(LabelTotal.Text);
// Calculate the percentage
double percentage = (double)label1Value / totalValue * 100;
// Set the percentage text to the Label1Percentage control
LabelPercentage.InnerText = percentage.ToString("F2") + "%"; // Format to 2 decimal places
}
But I am getting an error
Here is the error message
Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.FormatException: Input string was not in a correct format.
Source Error:
Line 75: // Convert the label text to integers
Line 76: int label1Value = Convert.ToInt32(Label1.Text);
Line 77: int totalValue = Convert.ToInt32(LabelTotal.Text);//This is where the error comes from.