Home > Software design >  Rounding multiple double numbers to two decimal places
Rounding multiple double numbers to two decimal places

Time:11-26

i'm new to programming, i decided to try and write this program, as an exercise, in the end it works fine without issues.

Below the third comment I'm initializing a variable performing calculation, after that statement, I'm rounding the value to two decimal places. Looking at that piece of code block, I'm repeating the two lines 7 times, and I'm thinking there is probably a better way to shorten that block of code.

double weightEarth, earthG, mercuryG, marsG, uranusG, venusG, saturnG, neptuneG, jupiterG;
            
//Assigning gravity force value corresponding to each planet
earthG = 9.81;
mercuryG = 3.7;
marsG = 3.711;
uranusG = 8.69;
venusG = 8.87;
saturnG = 10.44;
neptuneG = 11.15;
jupiterG = 24.79;

//Receive user input
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("This program will calculate how much you will weight on all planets");
Console.Write("Type in your weight in kg: ");
weightEarth = double.Parse(Console.ReadLine());

 //Calculating mass of object by dividing and gravitional force by multiplication
double weightMercury = (weightEarth / earthG) * mercuryG;
    weightMercury = Math.Round(weightMercury, 2);
double weightMars = (weightEarth / earthG) * marsG;
    weightMars = Math.Round(weightMars, 2);
double weightUranus = (weightEarth / earthG) * uranusG;
    weightUranus = Math.Round(weightUranus, 2);
double weightVenus = (weightEarth / earthG) * venusG;
    weightVenus = Math.Round(weightVenus, 2);
double weightSaturn = (weightEarth / earthG) * saturnG;
    weightSaturn = Math.Round(weightSaturn, 2);
double weightNeptune = (weightEarth / earthG) * neptuneG;
    weightNeptune = Math.Round(weightNeptune, 2);
double weightJupiter = (weightEarth / earthG) * jupiterG;
    weightJupiter = Math.Round(weightJupiter, 2);

Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\r\nYou weight:");
Console.WriteLine(weightMercury   "kg on Mercury");
Console.WriteLine(weightMars   "kg on Mars");
Console.WriteLine(weightUranus   "kg on Uranus");
Console.WriteLine(weightVenus   "kg on Venus");
Console.WriteLine(weightSaturn   "kg on Saturn");
Console.WriteLine(weightNeptune   "kg on Neptune");
Console.WriteLine(weightJupiter   "kg on Jupiter");

Console.ReadKey();

CodePudding user response:

Depending on how far you want to go, there are some concepts you can follow.

The first one is Don't Repeat Yourself (DRY). You could extract the weight calculation to a new method, like,

private static double GetWeightForPlanet(double weightEarth, double earthG, double planetG)
{
      double weightPanet = (weightEarth / earthG) * planetG;
      weightPanet = Math.Round(weightPanet, 2);
      return weightPanet;
}

Now your program will look like this,

//Calculating mass of object by dividing and gravitional force by multiplication
double weightMercury = GetWeightForPlanet(weightEarth, earthG, mercuryG);
double weightMars = GetWeightForPlanet(weightEarth, earthG, marsG);
double weightUranus = GetWeightForPlanet(weightEarth, earthG, uranusG);
double weightVenus = GetWeightForPlanet(weightEarth, earthG, venusG);
double weightSaturn = GetWeightForPlanet(weightEarth, earthG, saturnG);
double weightNeptune = GetWeightForPlanet(weightEarth, earthG, neptuneG);
double weightJupiter = GetWeightForPlanet(weightEarth, earthG, jupiterG);

Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("\r\nYou weight:");
Console.WriteLine(weightMercury   "kg on Mercury");
Console.WriteLine(weightMars   "kg on Mars");
Console.WriteLine(weightUranus   "kg on Uranus");
Console.WriteLine(weightVenus   "kg on Venus");
Console.WriteLine(weightSaturn   "kg on Saturn");
Console.WriteLine(weightNeptune   "kg on Neptune");
Console.WriteLine(weightJupiter   "kg on Jupiter");

There is still some repeating code, but I can see some common data patterns. Maybe a data structure will help; let's introduce the concept (class) Planet,

public class Planet
{
    public string Name { get; set; }
    public double Gforce { get; set; }
}

Your code will look like this,

double earthG = 9.81, weightEarth;

var planets = new List<Planet>
{
       new Planet { Name = "Mercury", Gforce = 3.7 },
       new Planet { Name = "Mars", Gforce = 3.711},
       new Planet { Name = "Uranus", Gforce = 8.69 },
       new Planet { Name = "Saturn", Gforce = 10.44 },
       new Planet { Name = "Neptune", Gforce = 11.15 },
       new Planet { Name = "Venus", Gforce = 8.87 },
       new Planet { Name = "Jupiter", Gforce = 24.79 }
};

//Receive user input
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("This program will calculate how much you will weight on all planets");
Console.Write("Type in your weight in kg: ");
weightEarth = double.Parse(Console.ReadLine());

Console.ForegroundColor = ConsoleColor.Blue;

Console.WriteLine("\r\nYou weight:");

foreach (var planet in planets)
{
     Console.WriteLine($"{GetWeightForPlanet(weightEarth, earthG, planet.Gforce)} kg on {planet.Name}");
}

I can keep going but probably stop here. This is for demonstration purposes only not for production code. Be kind on code reviews :)

  • Related