Question

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

Was it helpful?

Solution

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());

OTHER TIPS

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

Try

(double) vVisitors / o.TotalOrders;

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top