Found 35164 Articles for Programming

C# Program to determine the difference in hours between two dates

Samual Sam
Updated on 23-Jun-2020 07:24:08

8K+ Views

Set two dates.DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20); DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);Now, get the difference between two dates.TimeSpan ts = date2 - date1;Get the result i.e. the difference in hours.ts.TotalHoursLet us see the complete code.Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       DateTime date1 = new DateTime(2018, 7, 15, 08, 15, 20);       DateTime date2 = new DateTime(2018, 8, 17, 11, 14, 25);       TimeSpan ts = date2 - date1;       Console.WriteLine("No. of Hours (Difference) = {0}", ts.TotalHours);    } }OutputNo. of Hours (Difference) = 794.984722222222

C# Percent ("P") Format Specifier

karthikeya Boyini
Updated on 23-Jun-2020 07:26:04

11K+ Views

The percent ("P") format specifier is used to multiply a number by 100.It converts the number into a string representing a % (percentage).We have the following double type −double val = .975746;If we will set (“P”) format specifier, then the above would result as −97.57 %In the same way, using (“P1”), would only include a single vale after decimal-point.97.6%Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double val = .975746;       Console.WriteLine(val.ToString("P", CultureInfo.InvariantCulture));       Console.WriteLine(val.ToString("P1", CultureInfo.InvariantCulture));       Console.WriteLine(val.ToString("P4", CultureInfo.InvariantCulture));    } }Output97.57 % 97.6 % 97.5746 %

C# Fixed-Point (“F”) Format Specifier

George John
Updated on 23-Jun-2020 07:25:28

677 Views

The ("F") format specifier converts a number to a string of the following form −"-ddd.ddd…"Above, "d" indicates a digit (0-9).Let us see an example.Here, if we will set (“F3”) format specifier to add three values after decimal places, for let’s say, 212.212.000The following is another example −Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       int val;       val = 38788;       Console.WriteLine(val.ToString("F",CultureInfo.InvariantCulture));       val = -344;       Console.WriteLine(val.ToString("F3",CultureInfo.InvariantCulture));       val = 5656;       Console.WriteLine(val.ToString("F5",CultureInfo.InvariantCulture));    } }Output38788.00 -344.000 5656.00000

C# Exponential (“E”) Format Specifier

karthikeya Boyini
Updated on 23-Jun-2020 07:15:36

769 Views

The ("E") format specifier converts a number to a string of the following form −"-d.ddd…E+ddd"Or"-d.ddd…e+ddd"Above, "d" is a digit (0-9).Prefix the exponent with an "E" or an "e".Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double d = 3452.7678;       Console.WriteLine(d.ToString("E", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("E10", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("e", CultureInfo.InvariantCulture));       Console.WriteLine(d.ToString("e10", CultureInfo.InvariantCulture));    } }Output3.452768E+003 3.4527678000E+003 3.452768e+003 3.4527678000e+003

Implicit conversion from 64-bit signed integer (long) to Decimal in C#

Samual Sam
Updated on 23-Jun-2020 07:17:31

170 Views

The long type represents a 64-bit signed integer.To implicitly convert a 64-bit signed integer to a Decimal, firstly set a long value.long val = 989678876876876;To convert long to decimal, assign the value.dec = val;Let us see another example −Example Live Demousing System; public class Demo {    public static void Main() {       long val = 76755565656565;       decimal dec;       Console.WriteLine("Implicit conversion from 64-bit signed integer (long) to Decimal");       dec = val;       Console.WriteLine("Decimal : "+dec);    } }OutputImplicit conversion from 64-bit signed integer (long) to Decimal Decimal : 76755565656565

C# Program to skip elements from a sequence as long as the specified condition is true

Chandu yadav
Updated on 23-Jun-2020 07:17:02

156 Views

Use the SkipWhile() method to skip elements from a sequence as long as the specified condition is true.The following is the array −int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 };Here is the condition.s => s >= 50As long as the above condition is true, the elements above 50 are skipped as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 35, 42, 48, 88, 55, 90, 95, 85 };       // skips elements above 50       IEnumerable selMarks = marks.AsQueryable().OrderByDescending(s => s).SkipWhile(s => s >=          50);       // displays rest of the elements       Console.WriteLine("Skipped marks > 60...");       foreach (int res in selMarks) {          Console.WriteLine(res);       }    } }OutputSkipped marks > 60... 48 42 35

C# Program to skip elements of an array from the end

karthikeya Boyini
Updated on 23-Jun-2020 07:18:00

882 Views

Declare an array and initialize elements.int[] marks = { 45, 50, 60, 70, 85 };Use the SkipLast() method to skip elements of an array from the end.IEnumerable selMarks = marks.AsQueryable().SkipLast(3);The elements are skipped and rest of the elements is returned as shown below −Example Live Demousing System; using System.Linq; using System.Collections.Generic; public class Demo {    public static void Main() {       int[] marks = { 45, 50, 60, 70, 85 };       Console.WriteLine("Array...");       foreach (int res in marks)       Console.WriteLine(res);       IEnumerable selMarks = marks.AsQueryable().SkipLast(3);       Console.WriteLine("Array after skipping last 3 elements...");       foreach (int res in selMarks)       Console.WriteLine(res);    } }OutputArray... 45 50 60 70 85 Array after skipping last 3 elements... 45 50

C# DateTime to add days to the current date

George John
Updated on 23-Jun-2020 07:18:39

6K+ Views

Firstly, get the current date.DateTime.TodayNow, use AddDays() method to add days to the current date. Here, we are adding 10 days to the current date.DateTime.Today.AddDays(10)Let us see the complete code −Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       Console.WriteLine("Today = {0}", DateTime.Today);       Console.WriteLine("Add 10 Days = {0}", DateTime.Today.AddDays(10));    } }OutputToday = 9/11/2018 12:00:00 AM Add 10 Days = 9/21/2018 12:00:00 AM

Convert.ToSingle Method in C#

Samual Sam
Updated on 23-Jun-2020 07:19:07

775 Views

Convert a specified value to a single-precision floating-point number using the Convert.ToSingle() method in C#.Here is our bool −bool boolVal = false;Now, let us use the ToSingle() method to convert the value to a single precision floating-point.float floatVal; floatVal = Convert.ToSingle(boolVal);Example Live Demousing System; public class Demo {    public static void Main() {       bool boolVal = false;       float floatVal;       floatVal = Convert.ToSingle(boolVal);       Console.WriteLine("Converted {0} to {1}", boolVal, floatVal);    } }OutputConverted False to 0

Full Date Short Time ("f") Format Specifier in C#

Ankith Reddy
Updated on 23-Jun-2020 07:19:26

124 Views

The Full Date Short Time standard format specifier represents a combination of the long date ("D") and short time ("t") patterns.UseDateTime to set the date.DateTime myDate = new DateTime(2018, 8, 29, 1, 10, 0);Now, use the (“f”) format specifier like this −myDate.ToString("f", CultureInfo.CreateSpecificCulture("en-US"))The following is an example −Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime myDate = new DateTime(2018, 8, 29, 1, 10, 0);       Console.WriteLine(myDate.ToString("f",CultureInfo.CreateSpecificCulture("en-US")));    } }OutputWednesday, August 29, 2018 1:10 AM

Advertisements