0

I've tried to make my tool display a field into % by doing some math then converting it.

Here is my code and what I would like to do is these things.

If the result is a value that comes out to a flat % then display the flat % example result of calculation comes out to 3% then show 3% otherwise if it's a decimal show 3.1% and one for if the value becomes less than 1% so .5 or .05 for example.

A little background for the code. the value tbOptionlvl5 will have a value out of 10,000 . 10,000 being 100% so the example might be lets say 510.

    if (Item_Is_Rare() && int.Parse(TbOptionID5.Text) >= 0)
    {
        lblProb5.Text = "";
        decimal result = ((int.Parse(TbOptionLvl5.Text) * 100) / 10000);
        lblProb5.Text = Convert.ToString(result) + "%";
    }

here is my code for calling the function

if (TbOptionID10.Text == "-1")
        lblProb10.Text = "";
        if (TbOptionID10.Text != "-1" && TbOptionID10.Text != "")
        {
            if (int.Parse(TbOptionID10.Text) > 10000)
                TbOptionID10.Text = Convert.ToString(10000);
            Update_Probability_Text();
        }
rene
  • 41,474
  • 78
  • 114
  • 152
  • "if the value becomes less than 1% so .5 or .05" - can you be a bit more clear on your expectations? Maybe just give a few explicit examples – devNull Sep 05 '20 at 23:45
  • So far it sounds like you just want to display decimals without trailing zeros: https://stackoverflow.com/q/3104615/5803406 – devNull Sep 05 '20 at 23:46
  • example i want the value to display simply the % so example would be if tbOptionLvl5.text has a value of 30 you would do 30*100 = 3,000 /10,000 would give you .3% my original code wont display .3% it would instead not display any % so 0% – Anthony Kennedy Sep 05 '20 at 23:47
  • here is a video of it in action notice the % isn't displaying correctly. https://gyazo.com/3f515f2fddba4571975a9b51d23e2562 – Anthony Kennedy Sep 05 '20 at 23:48
  • From the linked question: "You're doing integer division, from the sounds of it." – devNull Sep 05 '20 at 23:55
  • 1
    it appears to be solved when i change the number /10000 to /1000m thanks for the help! – Anthony Kennedy Sep 06 '20 at 00:06

2 Answers2

-1
StringBuilder builder = new StringBuilder(result.ToString("##.##"));
            builder.Append(" %");
            lblProb5.Text = builder.ToString();
Mohammed
  • 151
  • 1
  • 12
-1

use a decimal specifier during the math.

decimal result = (tbText.text * 100) /10000

Fixed is as follows

 decimal result = (tbText.text * 100) /10000m