-1

I got this (C#) :

Random RNG = new Random();
decimal divab50 = RNG.Next(50,100);
decimal divbl50 = RNG.Next(6,50);
decimal decreturn = divab50 / divbl50;
Console.WriteLine(decreturn);

How can I round the decreturn var to two decimals? I've tried Math.Round and String.Format they don't seem to work for vars generated in RNG. I think. I'm new at c# just started

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • `Console.WriteLine()` doesn't know or care that `decreturn` happened to come from a `Random`; all it sees is the value passed to it. Also, for future reference it'd be helpful to include what you already tried in the question because `Math.Round()` and `String.Format()` (which uses the same formatting as `Console.WriteLine()`) are the two common ways to do this (`decimal.ToString()` being a third). – Lance U. Matthews Mar 20 '20 at 23:46

3 Answers3

0

If you take your variable decreturn and do Math.Round(decreturn, 2) or String.Format("{0:F2}", decreturn), it works as expected.

The following example is working:

using System;

public class Program
{
    public static void Main()
    {
        Random RNG = new Random();
        decimal divab50 = RNG.Next(50,100);
        decimal divbl50 = RNG.Next(6,50);
        decimal decreturn = divab50 / divbl50;
        decimal rounded = Math.Round(decreturn, 2);
        Console.WriteLine(rounded);
    }
}

Fiddle to test with Math.Round: https://dotnetfiddle.net/70LTrm

You could also apply String.Format for this purpose like this:

using System;

public class Program
{
    public static void Main()
    {
        Random RNG = new Random();
        decimal divab50 = RNG.Next(50,100);
        decimal divbl50 = RNG.Next(6,50);
        decimal decreturn = divab50 / divbl50;
        var rounded = String.Format("{0:F2}", decreturn);
        Console.WriteLine(rounded);
    }
}

Fiddle to test with String.Format: https://dotnetfiddle.net/6Yy8uU

Take a look to the documentation of Math.Round and String.Format for more info.

piraces
  • 1,320
  • 2
  • 18
  • 42
0

Use either Math.Round or an appropriate format specifier depending on your needs

Rounds a double-precision floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.

The fixed-point ("F") format specifier converts a number to a string of the form "-ddd.ddd…" where each "d" indicates a digit (0-9). The string starts with a minus sign if the number is negative.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the current NumberFormatInfo.NumberDecimalDigits property supplies the numeric precision.

// this should be a static or an instance field
Random RNG = new Random();

// inside a method
decimal divab50 = RNG.Next(50,100);
decimal divbl50 = RNG.Next(6,50);
decimal decreturn = divab50 / divbl50;
Console.WriteLine(Math.Round(decreturn,2));
Console.WriteLine($"{decreturn:F2}");   

Example Output

3.42
3.42

Online Demo

Note : Creating a new instance of Random every time you need a random number will cause you issues. It will be best to use this as a static field or an instance member

TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • I just instanced Random once, and use it every time I need a number generated, is this not recommended? I'm learning on a 6 year-old course on YT. Thank you for the answer and the note – Andrei Ion Mar 20 '20 at 23:34
  • @AndreiIon for further information and clarification on Random see https://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number – TheGeneral Mar 20 '20 at 23:38
0

Thank you for your answer everyone.

What worked for me was adding "{0:F2}", to the WriteLine Method like this :

Random RNG = new Random();
decimal divab50 = RNG.Next(50,100);
decimal divbl50 = RNG.Next(6,50);
decimal decreturn = divab50 / divbl50;
Console.WriteLine("{0:F2}",decreturn);

This also worked. I wasn't doing it right the first time.

decimal rounded = Math.Round(decreturn, 2);
Console.WriteLine(rounded);

Thank you everyone