0

Im working on a method calculating conversion rate, problem is i devide Visitors with orders and get a result (whole numbers like 84) but when i divide the other way around i get 0.0.

If i devide orders with visits with a calculator the result looks something like this:

0,0118694362017804

However i have to display the result as percent so i should shorten the result to 00,00 somehow. Annyways its strange it dosn't work both ways and im wondering what's the problem?

Controller Method:

 public List<GC_ConversionRateModel> GetConversionReport(GAStatisticsListModel model)
        {

            DateTime? startDateValue = (model.StartDate == null) ? null
                            : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.StartDate.Value, _dateTimeHelper.CurrentTimeZone);

            DateTime? endDateValue = (model.EndDate == null) ? null
                            : (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.EndDate.Value, _dateTimeHelper.CurrentTimeZone).AddDays(1);




            List<GAVisitorsModel> VisitorsList = GetGAStatisticsReport(model);
            List<GC_OrdersModel> OrdersList = GetOrderReport(model);
            List<GC_ConversionRateModel> TotalConversions = new List<GC_ConversionRateModel>();

            OrdersList.ForEach(o =>
            {
                TotalConversions.Add((from v in VisitorsList
                                      where v.Date == o.Date
                                      select new GC_ConversionRateModel(o.Date, v.Visitors / o.TotalOrders)).FirstOrDefault());

            });

            return TotalConversions;
        }

These are the object classes:

GAVisitorsModel:

public class GAVisitorsModel : IGAVisitorsModel
    {

        public string Date { get; set; }
        public int Visitors { get; set; }

        public GAVisitorsModel(string _date, string _visitors)
        {

            Date = _date;
            Visitors = _visitors;

        }

    }

GCOrdersModel:

 public class GC_OrdersModel
    {

        public string Date { get; set; }
        public int TotalOrders { get; set; }
        public int TotalProducts {get; set;}



        public GC_OrdersModel(string _date, int _totalOrders, int _totalProducts)
        {
            Date = _date;
            TotalOrders = _totalOrders;
            TotalProducts = _totalProducts;
        }

    }

GC_ConversionRateModel:

 public class GC_ConversionRateModel
    {

        public string Date { get; set; }
        public decimal ConversionRate { get; set; }



        public GC_ConversionRateModel(string _date, decimal _conversionRate)
        {
            Date = _date;
            ConversionRate = _conversionRate;

        }

    }

Thx

WhoAmI
  • 1,188
  • 6
  • 17
  • 47
  • the same question again and again... – Selman Genç Apr 14 '14 at 12:00
  • possible duplicate of [C#: divide an int by 100](http://stackoverflow.com/questions/5242436/c-divide-an-int-by-100) or [C#: int division without decimals](http://stackoverflow.com/questions/11568986/c-sharp-int-division-without-decimals) or [C#: Division returns zero](http://stackoverflow.com/questions/9288904/division-returns-zero) – Selman Genç Apr 14 '14 at 12:02

3 Answers3

3

You are dividing an int by an int, which results in an int - meaning you do lose most of the precision. You want to do a float/double division, so cast your variables to that.

select new GC_ConversionRateModel(o.Date, (float)v.Visitors / (float)o.TotalOrders)).FirstOrDefault());
Baldrick
  • 11,712
  • 2
  • 31
  • 35
kat0r
  • 949
  • 8
  • 17
2

If you divide an int by an int, the result will be an int.

Try

(double) vVisitors / o.TotalOrders;
Rik
  • 28,507
  • 14
  • 48
  • 67
0

Your problem is that result of your operation is intended to be an integer, so value is truncated.

Example:

7/2 = 2; //7 is integer devided on integer 2 == integer 2;

but if you write, that at least one of them is a decimal, the result will inferred by compiler as decimal

7/2.0 = 2; //7 is integer devided on decimal 2 == decmal 2.333;

So in your concrete case you may do like:

var decimalResult = visitors_count/(decimal)oders_count;
Tigran
  • 61,654
  • 8
  • 86
  • 123