Calculate and display percentage value from value of two label controls

Donald Symmons 3,066 Reputation points
2025-12-15T22:26:56.9966667+00:00

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**


```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 (%) = <asp:Label runat="server" ID="Label1Percentage" Text=""></asp:Label>

C#

protected void Page_Load(object sender, EventArgs e)
        {
                Percent();
        }
      private void Percent()
        {
            string Percent = Convert.ToInt32(Label1.Text) / Convert.ToInt32(LabelTotal.Text) * 100.ToString();
            Label1Percentage.Text = Percent;
        }

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-12-15T22:27:09.3733333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To calculate and display the percentage value from the value of a label control in ASP.NET, you need to ensure that the calculation is performed correctly and that the result is formatted properly. In your provided C# code, there are a couple of issues that need to be addressed:

    1. Integer Division: When you divide two integers in C#, the result is also an integer. To get a decimal result, at least one of the operands must be a floating-point number.
    2. String Conversion: You should convert the result to a string after the calculation, not before.

    Here’s how you can modify your Percent method:

    private void Percent()
    {
        // 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
        Label1Percentage.Text = percentage.ToString("F2") + "%"; // Format to 2 decimal places
    }
    

    Explanation:

    • Casting to Double: By casting label1Value to double, you ensure that the division results in a decimal value.
    • Formatting: The ToString("F2") method formats the number to two decimal places, and you can append a percentage sign to it.

    This will display the percentage of Label1 relative to LabelTotal in the Label1Percentage control correctly.

    0 comments No comments

  2. Raymond Huynh (WICLOUD CORPORATION) 3,955 Reputation points Microsoft External Staff Moderator
    2025-12-16T04:08:28.6333333+00:00

    Hello Donald Symmons,
    Your current code isn’t returning the right percentage because of how C# handles integer division and string conversion.

    In this line you wrote:

    string Percent = Convert.ToInt32(Label1.Text) / Convert.ToInt32(LabelTotal.Text) * 100.ToString();
    

    Two things happen here:

    • Convert.ToInt32(Label1.Text) / Convert.ToInt32(LabelTotal.Text) uses integer division, so 6/27 becomes 0 because both sides are ints.
    • You call .ToString() on 100 before doing any math, so C# sees it as a string ­× number rather than real arithmetic.

    To fix it, do the math with actual decimal numbers first, then convert the result to a string for display. For example:

    int part = int.Parse(Label1.Text);int total = int.Parse(LabelTotal.Text);
    if (total > 0)
    {
        double percentage = (double)part / total * 100;
        Label1Percentage.Text = percentage.ToString("0.##") + "%";
    }
    

    Here, casting one operand to double ensures C# does real decimal division instead of integer division. That gives you something like 22.22% instead of 0%

    You can repeat the same pattern for the other labels if you want percentages for all of them.
    Hope this helps!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.