Found 34490 Articles for Programming

Decimal type in C#

Samual Sam
Updated on 22-Jun-2020 14:48:59

2K+ Views

The decimal type is a value type and has the plus, minus, multiply and divide operators.Firstly, set two decimal values −decimal d1 = 5.8M; decimal d2 = 3.2M;To add decimals −d1 = d1 + d2;Let us see an example to add two decimal values −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       decimal d1 = 5.8M;       decimal d2 = 3.2M;       d1 = d1 + d2;       Console.WriteLine(d1);    } }Output9.0

Round a number to the nearest even number in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:49:23

607 Views

The ToEven property is used with the MidpointRounding Enumeration to round a number to the nearest even number.Declare and initialize a decimal number −decimal val = 25.55M;To round a number to the nearest even number −decimal.Round(val, 0, MidpointRounding.ToEven)Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       decimal val = 25.55M;       Console.WriteLine(decimal.Round(val, 0, MidpointRounding.ToEven));    } }Output26

C# program to remove all the numbers after decimal places

Samual Sam
Updated on 22-Jun-2020 14:49:48

4K+ Views

Use the Truncate method in C# to remove all the number after decimal places.Let’s say the following is our number −9.15MTo remove the numbers after decimal places, use Truncate() −decimal.Truncate(9.15M)Let us see the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       decimal val = 9.15M;       Console.WriteLine(decimal.Truncate(val));    } }Output9

Long.parse method in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:50:11

1K+ Views

To convert a string to a long, use the Long.parse method in C# −Firstly, set a string −string str = "7864646475767";Now, convert it to long −long.Parse(str);Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       string str = "7864646475767";       long res = long.Parse(str);       Console.WriteLine(res);    } }Output7864646475767

Bool.parse method in C#

Samual Sam
Updated on 22-Jun-2020 14:50:31

118 Views

To convert a string to a bool, use the Bool.parse method in C# −Firstly, set a string −string str = "true";Now, convert it to bool −bool.Parse(str);Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       string str = "true";       bool res = bool.Parse(str);       Console.WriteLine(res);    } }OutputTrue

Group by Operator in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:51:03

123 Views

Use the group by the operator in C# to separate the results of an expression into parts.Let’s say the following is our array −int[] a = { 5, 10, 15, 20, 25, 30 };Now, using Group by and orderby, we will find the elements greater than 20 −var check = from element in a orderby element group element by chkGreater(element);The following is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] a = { 5, 10, 15, 20, 25, 30 };       var check = from element ... Read More

C# program to check whether two sequences are the same or not

Samual Sam
Updated on 22-Jun-2020 14:51:28

140 Views

The SequenceEqual method is used to test collections for equality.Set sequences −string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This", "is", "it" };Now, use the SequenceEquals methods to check whether sequences are same or not −arr1.SequenceEqual(arr2);The following is an example −Example Live Demousing System; using System.Linq; class Program {    static void Main() {       string[] arr1 = { "This", "is", "it" };       string[] arr2 = { "My", "work", "report" };       string[] arr3 = { "This", "is", "it" };       bool res1 = arr1.SequenceEqual(arr2);       Console.WriteLine(res1);       bool res2 = arr1.SequenceEqual(arr3);       Console.WriteLine(res2);    } }OutputFalse True

C# Program to find the largest element from an array

karthikeya Boyini
Updated on 22-Jun-2020 14:51:48

2K+ Views

Declare an array −int[] arr = { 20, 50, -35, 25, 60 };Now to get the largest element from an array, use the Max() method −arr.Max());Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 20, 50, -35, 25, 60 };       Console.WriteLine(arr.Max());    } }Output60

C# Program to check if a character is a whitespace character

Samual Sam
Updated on 22-Jun-2020 14:52:19

246 Views

Set a character with WhiteSpace −char c = ' ';To check if a character is a whitespace character, use the char.IsWhiteSpace method −if (char.IsWhiteSpace(c)) {}Let us see the complete code −Example Live Demousing System; class Demo {    static void Main() {       char c = ' ';       if (char.IsWhiteSpace(c)) {          Console.WriteLine("Whitespace character!");       }    } }OutputWhitespace character!

Set 6-item tuple in C#’

Chandu yadav
Updated on 22-Jun-2020 14:44:48

66 Views

With C#, you can easily set a 6-item tuple.The following is a 6-item tuple −var myTuple = new Tuple("electronics", new string[] { "shoes", "clothing#", "accessories" }, 100, 250, 500, 1000);above, we have tuple for string, string array and int as shown below −TupleHere is the complete code −Example Live Demousing System; class Demo {    static void Main() {       var myTuple = new Tuple("electronics",       new string[] { "shoes", "clothing#", "accessories" },       100,       250,       500,       1000);       // Displaying Item 1       Console.WriteLine(myTuple.Item1);       // Displaying Item 5       Console.WriteLine(myTuple.Item5);       // Displaying Item 6       Console.WriteLine(myTuple.Item6);    } }Outputelectronics 500 1000

Advertisements