Write a C# Program to convert a Double to an Integer Value

 using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

using System.Threading;

using System.IO;


namespace StockTracker

{

    class AkbTechie

     {

            static void Main(string[] args)

                {


                   // Using Typecasting

                    double d = 1256.85;

                    int i = 0;

                    i = (int)d;   // It Truncates decimal part

                    Console.WriteLine(i);   //output:-1256



                   //using Math.Round() method 

                    double d1 = 1256.85;

                    int i1 = (int)Math.Round(d1);  //Math.Round method rounds a double value to the nearest integer.

                    Console.WriteLine(i1); //output:-1257



                    //using Math.Floor() method 

                    double d2 = 1256.85;

                    int i2 = (int)Math.Floor(d2);  //Math.Floor() method  Rounds towards negative infinity

                    Console.WriteLine(i2); //output:1256


                    //using Math.Ceiling() method 

                    double d3 = 1256.85;

                    int i3 = (int)Math.Ceiling(d3);  // Math.Ceiling() method  Rounds towards positive infinity

                    Console.WriteLine(i3); //output :-1257



                    //using  Convert.ToInt32()

                    double d4 = 1256.85;                 

                    int val = Convert.ToInt32(d4); // It rounds the value towards zero.

                    Console.WriteLine(val);  //output:-1257

                   Console.ReadKey();           

            }

    }

}

एक टिप्पणी भेजें

0 टिप्पणियाँ