Math.Min() Method in C#


The Math.Min() method in C# is used to return the larger of two specified numbers. This method works for both the numbers being Double, Decimal, Int16, Int32, etc.

Syntax

Following is the syntax −

public static ulong Min (ulong val1, ulong val2);
public static uint Min (uint val1, uint val2);
public static ushort Min (ushort val1, ushort val2);
public static float Min (float val1, float val2);
public static byte Min (byte val1, byte val2);
public static long Min (long val1, long val2);

Example

Let us now see an example to implement Math.Min() method −

using System;
public class Demo {
   public static void Main(){
      byte val1 = 10, val2 = 15;
      decimal val3 = 1000M, val4 = 1500M;
      double val5 = 15.896745, val6 = 25.676843;
      short val7 = 20, val8 = 30;
      Console.WriteLine("Minimum Value from two byte values = "+Math.Min(val1, val2));
      Console.WriteLine("Minimum Value from two decimal values = "+Math.Min(val3, val4));
      Console.WriteLine("Minimum Value from two double values = "+Math.Min(val5, val6));
      Console.WriteLine("Minimum Value from two short values = "+Math.Min(val7, val8));
   }
}

Output

This will produce the following output −

Minimum Value from two byte values = 10
Minimum Value from two decimal values = 1000
Minimum Value from two double values = 15.896745
Minimum Value from two short values = 20

Example

Let us see another example to implement Math.Min() method −

using System;
public class Demo {
   public static void Main(){
      int val1 = 10, val2 = 15;
      float val3 = 12.8f, val4 = 25.6f;
      Console.WriteLine("Minimum Value from two int values = "+Math.Min(val1, val2));
      Console.WriteLine("Minimum Value from two float values = "+Math.Min(val3, val4));
   }
}

Output

This will produce the following output &minuns;

Minimum Value from two int values = 10
Minimum Value from two float values = 12.8

Updated on: 06-Nov-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements