Home > Back-end >  How would i remove the zeros from printing (i'm not sure how to phrase this) (C#)
How would i remove the zeros from printing (i'm not sure how to phrase this) (C#)

Time:10-22

How would I remove the 0's from the "Days Overdue" and "Fine" columns?

I appreciate any and all comments

My outcome is

Book ID         Borrower ID     Date Borrowed   Date Returned   Days Loan       Days Overdue    Fine
0123            S10011122A      02/02/2020      06/02/2020      4               0               $0
3322            S10011126B      12/02/2020      13/02/2020      1               0               $0
1234            S10097878C      11/02/2020      01/03/2020      19              5               $2.5
8123            S10011122A      02/02/2020      06/03/2020      33              19              $9.5
3992            S10011126B      06/02/2020      09/02/2020      3               0               $0
1434            S10097878C      11/02/2020      01/03/2020      19              5               $2.5

In my code (shown below), I intend to only show the ones that are overdue (ie more than 14 days), meaning that the 0's you see should be blank. Is this possible? If not then thanks for checking out my post :)

using System;
using System.IO;

    namespace S10221788_Q2
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] csvLines = File.ReadAllLines("loaninfo.csv");

                string[] heading = csvLines[0].Split(',');
                Console.WriteLine("{0,-15} {1,-15} {2,-15} {3,-15} {4,-15} {5,-15} {6,-15}", heading[0], heading[1], 
                                   heading[2], heading[3], "Days Loan", "Days Overdue", "Fine");

                for (int i=1; i<csvLines.Length; i  )
                {
                    string[] id = csvLines[i].Split(',');
                    DateTime borrowdate = Convert.ToDateTime(id[2]);
                    DateTime returndate = Convert.ToDateTime(id[3]);
                    int loan = returndate.Subtract(borrowdate).Days;
                    int overdue = loan - 14;
                    if (overdue < 0)
                    {
                        overdue = 0;
                    }
                    string fine = Convert.ToString((double)(overdue * 0.5));
                    Console.WriteLine("{0,-15} {1,-15} {2,-15} {3,-15} {4,-15} {5,-15} ${6,-15}",
                                       id[0], id[1], borrowdate.ToString("dd/MM/yyyy"), returndate.ToString("dd/MM/yyyy"), loan, overdue, fine);
                }
            }
        }
    }

CodePudding user response:

Simple, change you line

Console.WriteLine("{0,-15} {1,-15} {2,-15} {3,-15} {4,-15} {5,-15} ${6,-15}",
                                       id[0], id[1], borrowdate.ToString("dd/MM/yyyy"), returndate.ToString("dd/MM/yyyy"), loan, overdue, fine);

To

Console.WriteLine("{0,-15} {1,-15} {2,-15} {3,-15} {4,-15} {5,-15} ${6,-15}",
                                       id[0], id[1], borrowdate.ToString("dd/MM/yyyy"), returndate.ToString("dd/MM/yyyy"), loan, 
overdue == 0 ? "" : overdue.ToString(), 
overdue == 0 ? "" : fine);
  •  Tags:  
  • c#
  • Related