C#中的模数和除法运算符 [英] Modulo and division Operator in C#

查看:315
本文介绍了C#中的模数和除法运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按天数计算金额,如考虑年,月,弱,日但它没有返回正确的值



我的代码是,请建议我纠正如果有的话



I want to calculate Amount by Number of days, as Considering year,month,weak,day but it does not return correct value

my code is ,Please suggest me correction if any

double year = 0, month = 0, weak = 0, day= 0;
               if (delay > 365)
                    year = delay % 365;
               if (delay < 365 && delay >= 31)
                   month = delay % 30;
               if (delay < 30 && delay >= 7)
                   weak = delay % 7;
               if (delay < 7)
                   day = delay % 1;
                        
            
               double year1=0, month1=0, weak1=0, day1=0;

                 year1 = delay / 365;
                 month1 = year / 12;
                 weak1 = month / 7;
                 day1 = day / 1;

           double yearVal = 100;
           double monthVal = 50;
           double weakVal = 10;
           double dayVal = 1;

           double Addition = year1 * yearVal + month1 * monthVal + weak1 * weakVal + day1 * dayVal;

推荐答案

你可以像这样计算天数/月数/年数:



You can calculate the number of days/months/years like this:

int years, months, weeks, days;
years = months = weeks = days = 0;

while (delay > 365)
{ years++; delay -= 365; }

while (delay > 30)
{ months++; delay -= 30; }

while (delay > 7)
{ weeks++; delay -= 7; }

days = delay;

int yearMult = 100;
int monthMult = 50;
int weekMult = 10;
int dayMult = 1;

int addition = (years * yearMult) + (months * monthMult) + (weeks * weekMult) + (days * dayMult);

//So for 400 as the "delay" that should give you 1 year, 1 month, 0 weeks and 5 days, and "addition" will equal 155


这篇关于C#中的模数和除法运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆