-1

The followings are some parts of my code. I use the calcengine library for doing mathematical operations in three textboxes. I want to get a rounded number in labelSTDW.Text but the result is an integer. target1 has an integer value but target1/60 can have a double value. My problem is that labelSTDW.Text. The text has always an integer value. How can I solve this problem?

 int target1, target2, target3;
        double STDWorker, STDTechnician, STDExpert;
        CalcEngine.CalcEngine CE = new CalcEngine.CalcEngine();
        int.TryParse(CE.Evaluate(textBoxTLA.Text).ToString(), out target1);
        int.TryParse(CE.Evaluate(textBoxTTE.Text).ToString(), out target2);
        int.TryParse(CE.Evaluate(textBoxTEX.Text).ToString(), out target3);
        TimeSpan timespanconversion = new TimeSpan(0, target1+target2+target3, 0);
        labelTmin.Text = $"Total Time: {timespanconversion.Days}D-{timespanconversion.Hours}H:{timespanconversion.Minutes}m";
        STDWorker = target1 / 60;
        STDTechnician = target2 / 60;
        STDExpert = target3 / 60;
        labelSTDW.Text = $"Worker Work Time: {Math.Round(STDWorker, 2)}";
Fardin
  • 66
  • 7
  • 1
    One of several good duplicates: [Why does integer division in C# return an integer and not a float?](https://stackoverflow.com/questions/10851273/why-does-integer-division-in-c-sharp-return-an-integer-and-not-a-float) – Ňɏssa Pøngjǣrdenlarp Oct 23 '20 at 17:15

1 Answers1

1

Before dividing by 60, convert target1 to double.

Convert.ToDouble(target1)/60 would give you double.

gsb22
  • 2,112
  • 2
  • 10
  • 25
  • 3
    Calling `Convert.ToDouble` is overkill. Cast it. Better yet, use a `double` literal and the variable will be promoted to `double`, e.g. `target / 60.0` – madreflection Oct 23 '20 at 17:15