Hypister
Hypister

Reputation: 157

Display random number as percentage


I'm studying C# and I want to display the % sign after the random number showed in one label.
My code works well, but show only the number. I want the number with the percent sign:

Random randnum = new Random();
label_showRandNum.Text = randnum.Next(-1, 101).ToString();

I just don't know how to show the number with the percent sign (%).
I've tried to format the label, but without success. I also have a message to be shown when the number chosen is, for example, 10:

int number;
number = Convert.ToInt32(label_showRandNum.Text);
if (number == 10)
{
    MessageBox.Show("You have picked 10%!");
}

I think the percent sign will cause an exception when converting the variable.
I do not know what to do. I will apreciate any help.
Thanks in advance!

Upvotes: 0

Views: 503

Answers (3)

Habib
Habib

Reputation: 223312

You have multiple options.

First use Format p or P in ToString like:

Random randnum = new Random();
label_showRandNum.Text = randnum.Next(-1, 101).ToString("p");

But, the problem with this is that, format p results in number multiplied by 100 and then a percentage sign is put next to it.

Standard Numeric Format: "p" or "P"

Number multiplied by 100 and displayed with a percent symbol.

You can use Random.NextDouble method which produces values between 0 to 1, and then use that in your Label.

But the other option is:

You can concatenate the % Percentage sign with your label, and when you are parsing it you can remove it like:

label_showRandNum.Text = randnum.Next(-1, 101).ToString()
    + CultureInfo.CurrentCulture.NumberFormat.PercentSymbol;

This will result in Text holding value like 10%, For en-US culture.

Later when you are parsing the Text value to int you can do:

int number = int.Parse(label_showRandNum.Text.Replace
                         (CultureInfo.CurrentCulture.NumberFormat.PercentSymbol,
                         ""));

I would rather use CultureInfo.CurrentCulture.NumberFormat.PercentSymbol then hard coding % symbol. As this might differ depending on the culture.

Upvotes: 1

Matt Burland
Matt Burland

Reputation: 45155

Something like this:

To get a random number as a string with a % sign:

    Random randnum = new Random();
    string text = randnum.NextDouble().ToString("P0");   // note P0 will give you 
                                                         // zero decimal places
    Console.WriteLine(text);

To convert that to an int (or a double), you can do something like this:

    int value = int.Parse(text.Replace("%",""));
    Console.WriteLine(value);

Note that using the P format specifier will automatically multiple the number by 100, which is why I used NextDouble to produce a number between 0..1 to give an output between 0..100%. So if you want the original value back after parsing it, you'll need to divide it by 100 again.

Upvotes: 0

Dev
Dev

Reputation: 1020

Do you mean this??

 int number;
    number = Convert.ToInt32(label_showRandNum.Text);
    if (number == 10)
    {
        MessageBox.Show("You have picked "+number +"%!");
    }

Upvotes: 0

Related Questions