根据时间计算价格
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据时间计算价格相关的知识,希望对你有一定的参考价值。
在项目中遇到根据时间技算价格,记录下
最初版源码
/// <summary> /// 时间计算 /// </summary> /// <param name="startTime">订单创建时间</param> /// <param name="isFirst">首次</param> /// <param name="dayFirstPrice">一小时首次价格</param> /// <param name="dayPrice">每30分钟的价格</param> /// <param name="nightPrice">夜间价格</param> /// <returns></returns> public decimal GetSumPrice(DateTime startTime, bool isFirst, decimal dayFirstPrice, decimal dayPrice, decimal nightPrice) { //白天时间范围 var dayStartTime = DateTime.Parse(startTime.ToLongDateString() + " 07:30:00"); var dayEndTime = DateTime.Parse(startTime.ToLongDateString() + " 19:30:00"); //夜间时间范围 var nightStartTime = DateTime.Parse(startTime.ToLongDateString() + " 19:30:00"); var nightEndTime = DateTime.Parse(startTime.AddDays(1).ToLongDateString() + " 07:30:00"); //总价 decimal price = 0; //计算白天总分钟数 int dayMinutes = Convert.ToInt32(dayEndTime.Subtract(dayStartTime).TotalMinutes); //计算晚上总分钟数 int nightMinustes = Convert.ToInt32(nightEndTime.Subtract(nightStartTime).TotalMinutes); //结束时间已系统当前时间为准 var endTime = DateTime.Now; //停车总分钟数 int sumMinustes = Convert.ToInt32(endTime.Subtract(startTime).TotalMinutes); //白天开始 if (startTime > dayStartTime && startTime < dayEndTime) { //没有超过白天停车时间 if (sumMinustes <= dayMinutes) { //停车时间超过5分钟,或者不是今天第一次停车 if (sumMinustes > 5 || !isFirst) { price = GetDayPrice(sumMinustes, dayPrice, dayFirstPrice, isFirst); } } else { //第一天白天分钟数 int fristDayMinutes = Convert.ToInt32(dayEndTime.Subtract(startTime).TotalMinutes); //第一天金额 price = GetDayPrice(fristDayMinutes, dayPrice, dayFirstPrice, isFirst) + nightPrice; //减去第一天已算时间 sumMinustes = sumMinustes - fristDayMinutes - nightMinustes; while (sumMinustes > 5) { if (sumMinustes - dayMinutes <= 0) { price += GetDayPrice(sumMinustes, dayPrice, dayFirstPrice, isFirst); break; } else { price += GetDayPrice(dayMinutes, dayPrice, dayFirstPrice, isFirst) + nightPrice; sumMinustes = sumMinustes - dayMinutes - nightMinustes; } } } } else { //晚上开始停 price = nightPrice; if (sumMinustes > nightMinustes) { sumMinustes -= nightMinustes; while (sumMinustes > 5) { if (sumMinustes - dayMinutes <= 0) { price += GetDayPrice(sumMinustes, dayPrice, dayFirstPrice, isFirst); break; } else { price += GetDayPrice(dayMinutes, dayPrice, dayFirstPrice, isFirst) + nightPrice; sumMinustes = sumMinustes - dayMinutes - nightMinustes; } } } } return price; } /// <summary> /// 计算价格 /// </summary> /// <param name="minutes"></param> /// <param name="price"></param> /// <param name="fristPrice"></param> /// <param name="isFirst"></param> /// <returns></returns> public static decimal GetDayPrice(int minutes, decimal price, decimal fristPrice, bool isFirst) { //首次 if (minutes <= 60 && isFirst) { return fristPrice; } //半小时 else if (isFirst) { int len = (minutes - 60) / 30; len = (minutes - 60) % 30 != 0 ? len + 1 : len; return len * price + fristPrice; } //不是首次5分钟 else { int len = minutes / 30; len = minutes % 30 != 0 ? len + 1 : len; return len * price; } }
以上是关于根据时间计算价格的主要内容,如果未能解决你的问题,请参考以下文章
Android:根据列表视图中点击的项目计算价格。应用程序在总活动 Logcat 中崩溃:无效浮点数:“”