0

I'm trying to create a simple C# program (I'm a beginner) to convert days to weeks, months and years. However, the answer always appears in integers, not decimals. For example: 1200 days are equivalent to 3.2876 years, but the program returns only 3. I am using VSCode and .NET 6.0. I tried some output formatting but only got 3.00. Here is the code:

static void Main(string[] args)
        {
           string aux="";
           int daysEntrada=0;
           decimal monthSaida, yearsSaida, weeksSaida;
           Console.WriteLine("-----------------------------");
           Console.WriteLine("days conversor!");
           Console.Write("Enter the number of days: ");
           Console.WriteLine("\n-----------------------------");
           Console.Write("-> ");
           aux = Console.ReadLine();
           bool isInteiro = int.TryParse(aux, out daysEntrada);
           if(isInteiro == true){
               daysEntrada = int.Parse(aux);
               monthSaida = daysEntrada/30;
               yearsSaida = daysEntrada/365;
               weeksSaida = daysEntrada/7;
               Console.WriteLine($"{daysEntrada} days is equal to: {daysEntrada} days, {weeksSaida} weeks, {monthSaida} months ans {yearsSaida} years.");
           }else{
               Console.WriteLine("Error, type again.");
           }
        }

The output generates:

days conversor!
Enter the number of days:    
-----------------------------
-> 1200
1200 days is equal to: 1.200,00 days, 171 weeks, 40 months ans 3,00 years
  • integer divided by integer yields an integer result. There are several dozen posts here "discovering" the same problem. [Why does integer division in C# return an integer and not a float?](https://stackoverflow.com/questions/10851273) – Ňɏssa Pøngjǣrdenlarp Jun 12 '22 at 20:39
  • Thank you so much, Ňɏssa Pøngjǣrdenlarp! I'm going to open the link that you suggested and try to solve again! – Julia_Gubolin Jun 12 '22 at 20:48
  • 1
    When you figure out the floating-point calculations, bear in mind that there's a `System.DateTime` and `System.TimeSpan` classes which can calculate for you almost anything you want with dates and times. – montonero Jun 12 '22 at 20:49

2 Answers2

0

The following is a rule that is valid in C, C++, C# and many more: Division of two integers gives an integer so when you divide daysEntrada by 365 it returns the floor of the division( for example if it is 3.2876 it returns 3 if it's 3.99 it returns 3).

The solution in C# is to cast any of the values to decimal, I'm no expert in C# but this might work:

yearsSaida = daysEntrada/(decimal)365;

Also an useful article: How can I divide two integers to get a double?

0

Try using floats instead of ints and decimals. The original error was caused because your original data type was int so when you divided it it returned an int (non decimal). Here I used floats for all the data types as that way you don't have to cast any values (from int to decimal). Like this (changes were commented):

public static void Main(string[] args)
    {
       string aux="";
       float daysEntrada=0; // changed from int to float
       float monthSaida, yearsSaida, weeksSaida; // changed from decimal to float
       Console.WriteLine("-----------------------------");
       Console.WriteLine("days conversor!");
       Console.Write("Enter the number of days: ");
       Console.WriteLine("\n-----------------------------");
       Console.Write("-> ");
       aux = Console.ReadLine();
       bool isInteiro = float.TryParse(aux, out daysEntrada); // changed from int.tryparse to float.tryparse
       if(isInteiro == true){
           daysEntrada = float.Parse(aux); //changed from int to float
           monthSaida = daysEntrada/30;
           yearsSaida = daysEntrada/365;
           weeksSaida = daysEntrada/7;
           Console.WriteLine($"{daysEntrada} days is equal to: {daysEntrada} days, {weeksSaida} weeks, {monthSaida} months ans {yearsSaida} years.");
       }else{
           Console.WriteLine("Error, type again.");
       }
    }
Ryan LeRoy
  • 19
  • 2