0

Im calculating the ratios of positive ints, negative ints and zeros in an array. I want to output the ratio rounded to six decimals.

using System;

namespace PlussMinussRatio
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            int[] array = new int[] { -4, 3, -9, 0, 4, 1 };
            Solution(array);

        }
        public static void Solution(int[] arr)
        {
            float positive = 0, negative = 0, zero = 0;
            float positiveRatio = 0, negativeRatio = 0, zeroRatio = 0;
            float arrLength = arr.Length;
            for(int i = 0;i < arrLength; i++)
            {
                if(arr[i] < 0)
                {
                    negative++;
                }
                else if(arr[i] > 0)
                {
                    positive++;
                }
                else
                {
                    zero++;
                }
            }
            positiveRatio = positive / arrLength;
            negativeRatio = negative / arrLength;
            zeroRatio = zero / arrLength;
            Math.Round(positiveRatio, 6);
            Math.Round(negativeRatio, 6);
            Math.Round(zeroRatio, 6);
            //Console.WriteLine(positive + " " + negative + " " + zero);
            Console.WriteLine(positiveRatio + "\n" + negativeRatio + "\n" + zeroRatio);
        }
    }
}

This is what I get :

0.5 0.333333 0.166667 I want the 0.5 to be 0.500000.How can I do that?

spaceOyster
  • 3
  • 1
  • 4
  • Possible duplicate of [Using String Format to show decimal up to 2 places or simple integer](https://stackoverflow.com/questions/6951335/using-string-format-to-show-decimal-up-to-2-places-or-simple-integer) – Sohaib Jundi Jul 13 '19 at 11:21
  • Simple way is to multiply by 1000000 and cast to an integer. Then divide by 1000000.0 – jdweng Jul 13 '19 at 11:22
  • Multiply by 10^6, round, divide by 10^6? –  Jul 13 '19 at 11:22
  • @jdweng That would not be rounding though, only flooring –  Jul 13 '19 at 11:23
  • You have to assign the output: `positiveRatio = Math.Round(positiveRatio, 6);` Anyway, these are floats, so precision is not guaranteed. You would have to use `decimal` for precision. – Silvermind Jul 13 '19 at 11:25
  • floats have limited precision - If the number of decimal places is important, try using a `decimal` instead. – Manfred Radlwimmer Jul 13 '19 at 11:25

1 Answers1

0

You're ignoring the return value of Math.Round(). It returns the rounded value.

But you don't need Math.Round(), you need a format string:

Console.WriteLine(positiveRatio.ToString("0.000000");
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thanks that worked!! But my error also was that I forgot to assign the output of Math.Round() to a variable. – spaceOyster Jul 13 '19 at 11:37
  • 1
    @spaceOyster A good custom format string. A very related _standard_ format string is `positiveRatio.ToString("F6")` ("fixed, 6"). Nowadays you would often use an interpolated string instead of explicit `ToString`. Then the entire statement would be `Console.WriteLine($"{positiveRatio:0.000000}");` respectively `Console.WriteLine($"{positiveRatio:F6}");`. – Jeppe Stig Nielsen Jul 13 '19 at 11:44