In: Computer Science
C# using MindTap How do I round this up to 2 decimals: I need the total to round to two decimals example instead of the output to be 119.625 it needs to be 119.63
using System;
using static System.Console;
using System.Globalization;
class DemoJobs
{
static void Main()
{
Job job1 = new Job();
Job job2 = new Job();
Job job3 = new Job();
job1.Description = "wash windows";
job1.Time = 3.5;
job1.Hourly = 25.00;
job2.Description = "clean garden";
job2.Time = 2;
job2.Hourly = 18.50;
job3 = job1 + job2;
Display(job1);
Display(job2);
Display(job3);
}
internal static void Display(Job j)
{
WriteLine("Job : {0} is {1} per hour for {2} hours.\n Total = {3}",
j.Description, j.Hourly.ToString("C",
CultureInfo.GetCultureInfo("en-US")), j.Time, j.Total.ToString("C",
CultureInfo.GetCultureInfo("en-US")));
}
}
class Job
{
private string description;
private double time;
private double hourly;
private double total;
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public double Time
{
get
{
return time;
}
set
{
time = value;
CalcTotal();
}
}
public double Hourly
{
get
{
return hourly;
}
set
{
hourly = value;
CalcTotal();
}
}
public double Total
{
get
{
return total;
}
}
private void CalcTotal()
{
total = Time * Hourly;
}
public static Job operator +(Job j1,
Job j2)
{
Job temp = new Job();
temp.Description = j1.Description + " and " + j2.Description;
temp.Time = j1.Time + j2.Time;
temp.Hourly = (j1.Hourly + j2.Hourly) / 2;
return temp;
}
}
You can use Math.Round(doublevalue,n) where n=no of digits
using System;
using static System.Console;
class DemoJobs
{
static void Main()
{
Job job1 = new Job();
Job job2 = new Job();
Job job3 = new Job();
job1.Description = "wash windows";
job1.Time = 5.26;
job1.Hourly = 25.5641;
job2.Description = "clean garden";
job2.Time = 2;
job2.Hourly = 18.50;
job3 = job1 + job2;
Display(job1);
Display(job2);
Display(job3);
}
internal static void Display(Job j)
{
WriteLine("Job : {0} is {1} per hour for {2} hours.\n Total = {3}",
j.Description, Math.Round(j.Hourly,2), Math.Round(j.Time,2),
Math.Round(j.Total,2));
}
}
class Job
{
private string description;
private double time;
private double hourly;
private double total;
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public double Time
{
get
{
return time;
}
set
{
time = value;
CalcTotal();
}
}
public double Hourly
{
get
{
return hourly;
}
set
{
hourly = value;
CalcTotal();
}
}
public double Total
{
get
{
return total;
}
}
private void CalcTotal()
{
total = Time * Hourly;
}
public static Job operator +(Job j1,
Job j2)
{
Job temp = new Job();
temp.Description = j1.Description + " and " + j2.Description;
temp.Time = j1.Time + j2.Time;
temp.Hourly = (j1.Hourly + j2.Hourly) / 2;
return temp;
}
}
Expected output: