Sponsored Ad

Tuesday, July 26, 2011

Calculate Maximum of two Int – Decimal - long Numbers in C# | Math.Max()

C# provide a math function to calculate max of two numbers, This function have overloaded functions for long, int, float, decimal, short etc.

if you face any problem in finding the max value can use the below function.

Calculate Maximum of two int – Decimal - long Numbers in C# | Math.Max()

C# Code to Find Maximum of Two Numbers :

using System;

namespace DemoConsoleApplication
{
    class Max_of_Numbers
    {
        static void Main(string[] args)
        {
           Console.WriteLine(" Int Number Max: " +  Math.Max(3,4));
           Console.WriteLine(" Double Number Max: " + Math.Max(3.333, 4.444));
           Console.WriteLine(" Long Number Max: " + Math.Max(3L, 4L));
           Console.WriteLine(" Decimal Number Max: " + Math.Max(3.33M, 4.44M));

            Console.ReadLine();
        }
    }
}

Calculate Truncated Number from a Decimal Number in C#

If you want to truncate a given decimal number of double number, C# provide a function Math.Truncate() to return corresponding number.

The below code provide sample demo application for the same.

Calculate Truncated Number from a Decimal Number in C#

C# Example Code to truncate a given number:

using System;

namespace DemoConsoleApplication
{
    class Math_Truncate
    {
        static void Main(string[] args)
        {
            decimal dec_Number = 400.2222M;

            decimal Result = Math.Truncate(dec_Number);
            Console.WriteLine(" Truncated Number: " + Result);

            Console.ReadLine();
        }
    }
}

Sponsored Ad

More Related Articles

Website Update

Followers