简体   繁体   中英

Why isn't my label displaying the number?

So I know my calculations may be large. However, when I try to do calculate the input cubed of the textbox adjacent to it, it won't display the number but it will display the string "exp: ".

Is this concatenation not working? Instead, it's only displaying "EXP: " if it's too large.

Also, I tried it the other way around but it will display the number (even if it's large) but not " exp".

for (int i = 0; i < textBoxes.Length; i++)
{
    totalExp += Math.Pow(Convert.ToDouble(textBoxes[i].Text), 3.0) + 1;
    labels[i].Text = "Exp: " + (Math.Pow(Convert.ToDouble(textBoxes[i].Text), 3.0) + 1).ToString("#,###.#####");
}

The textbox properties were set on my last question ( https://stackoverflow.com/a/20406828/2804613 )

I did the exact same thing with the labels so that they are on the right side of the textbox.

I tried to reproduce your problem using the following code:

    double thisExp = Math.Pow(Convert.ToDouble("1.37"), 3.0) + 1;
    // totalExp += thisExp;
    string Text = "Exp: " + thisExp.ToString("#,###.#####");

    Console.WriteLine(Text);

It prints

Exp: 3.57135

You can try it out here

It is therefore not a formatting problem. I suspect the conversion from string to double, but we need more information for that. Especially locale settings and the exact string contents.

A second thought: Can you read back the label text? If you set the label size too narrow for the text, it may just display the first few characters.

Explaining my comment:

for (int i = 0; i < textBoxes.Length; i++)
{
    var exp = Math.Pow(Convert.ToDouble(textBoxes[i].Text), 3.0) + 1;
    totalExp += exp;
    labels[i].Text = "Exp: " + exp.ToString("#,###.#####");
}

Better way to do this is using String.Format()

for (int i = 0; i < textBoxes.Length; i++)
{
    var exp = Math.Pow(Convert.ToDouble(textBoxes[i].Text), 3.0) + 1;
    totalExp += exp;
    labels[i].Text = String.Format("Exp: {0:#,###.#####}", exp);
}

And make sure you set AutoSize property of the label to True .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM