0

I'm trying to create a map-related calculator and I'm having a problem with the Math.Round method. Basically, I want the program to take the real-life length and the length on a map to calculate the scale of said map. After it calculates the scale it should round it from a double to an int. So for example the real-life length is 3000000 cm and the on map length equals 8,5 cm now after dividing these we get 352 941,176 that's our scalenoteven in this context. Now after rounding it, the scale should be 1:352 941 but instead the program gives me a scale of 1:352.

 double Scalenoteven;
 int Scaleeven;

//RealLengthincm and Maplength are taken from the user 
  Scalenoteven = RealLengthincm / MapLength;
  Scaleeven = (int)Math.Round(Scalenoteven, 1, MidpointRounding.ToEven);
Hain_Z
  • 9
  • 3
  • What types are `RealLengthincm` and `MapLength`? Is it an `int` or a `double`? `Math.round` will not scale your input. What's up with the blank in the input? Have you double checked that `Scalenoteven` has the correct value after division? – knittl May 15 '22 at 17:58
  • This bit of code looks just fine, the problem might be how you are trying to display the value. – GuyVdN May 15 '22 at 17:59
  • this works for me. I think there's something missing in the sample code, e.g. how do you process the user's input. Are you using something like `int.Parse()`? Show the complete code please – M4N May 15 '22 at 18:00
  • @knittl both are doubles only Scaleeven is an int – Hain_Z May 15 '22 at 18:05
  • @GuyVdN I display it like this `Console.WriteLine("The Scale is 1:" + Scaleeven);` – Hain_Z May 15 '22 at 18:06
  • @M4N I use a string that takes the users input via Console.ReadLine and then I convert it to a double – Hain_Z May 15 '22 at 18:09
  • That looks fine as well, might be the input then like M4N suggested. Did you try debugging the code to get a look at the actual values being calculated? – GuyVdN May 15 '22 at 18:09
  • @GuyVdN Well here's the complete code maybe you can make something out of it https://pastebin.com/1H7avNFg – Hain_Z May 15 '22 at 18:19
  • Converting it to double can give you the wrong result if your culture is not set correctly. As you are using 8,5 in your question and not 8.5 that might be were the problem is at. You will be dividing by a greater number than you expected depending on the precision you provide. You might want to set a specific cultere like so: `var culture = new CultureInfo("de-DE"); var length = double.Parse(input, culture);` – GuyVdN May 15 '22 at 18:20

1 Answers1

0

So with the added culture info and RealLengthincm = RealLength * 100; this should be working.

using System.Globalization;

double RealLength;
string RealLengthString;
double MapLength;
string MapLengthString;
double RealLengthincm;
double Scalenoteven;
int Scaleeven;

Console.WriteLine("Firstly will the real length be in meters or kilometers?");
string Answer;
Answer = Console.ReadLine();

if (Answer == "meters")
{
    Console.WriteLine("Alright!");
    Console.WriteLine("So what's the real length?");

    var culture = new CultureInfo("de-DE");

    RealLengthString = Console.ReadLine(); // assuming 30000
    RealLength = double.Parse(RealLengthString, culture);

    RealLengthincm = RealLength * 100;
    Console.WriteLine("now what's the length on the map in cm");

    MapLengthString = Console.ReadLine(); // assuming 8,5
    MapLength = double.Parse(MapLengthString, culture);

    //RealLengthincm and Maplength are taken from the user 
    Scalenoteven = RealLengthincm / MapLength;
    Scaleeven = (int)Math.Round(Scalenoteven, 0, MidpointRounding.ToZero);
    Console.WriteLine("The Scale is 1:" + Scaleeven); // outputs 1:352941
}
GuyVdN
  • 690
  • 4
  • 14