Monday, October 17, 2022

[FIXED] How to multiply the number of two labels in C#

Issue

so i have 2 labels. one of them is a fixed number and doesn't change but the other one changes every 5 seconds. Now i want to multiply them automatically and show them in another label as Results.

what should i do? what am i doing wrong?

i tried this code but it says "operator * cannot be applied to string and string".

label1.Text = BTC_A.Text * BTCPrice_Label.Text;

then i tried

double txt1 = Convert.ToDouble(BTC_A.Text);
            double txt2 = Convert.ToDouble(BTCPrice_Label.Text);

            double sum = txt1 * txt2;

            label1.Text = sum.ToString();

but it says "Input string was not in a correct format"


Solution

so the problem was a dollar sign ( $ ) that i put before the numbers.

i just deleted the sign and this is what the code looks like now:

double AA;
           if (!double.TryParse(BTC_A.Text, out AA))
           {
                MessageBox.Show($"Unable to convert the BTC_A \"{BTC_A.Text}\" to a floating point number");
                return;
           }
           double btcA;
           if (!double.TryParse(BTCPrice_Label.Text, out btcA))
           {
                MessageBox.Show($"Unable to convert the price \"{BTCPrice_Label.Text}\" to a floating point number");
                return;
           }
           label1.Text = (AA * btcA).ToString();


Answered By - Oliver
Answer Checked By - Clifford M. (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.