Found 34494 Articles for Programming

C# Program to display temporary file names

karthikeya Boyini
Updated on 22-Jun-2020 14:55:40

154 Views

The GetTempPath() method in C# displays temporary file names −Path.GetTempPath();Get the names in a variable and display −string tempFile = Path.GetTempPath();The following is the code −Example Live Demousing System; using System.IO; class Demo {    static void Main() {       string tempFile = Path.GetTempPath();       Console.WriteLine(tempFile);    } }Output/tmp/

Manipulate decimals with numeric operators in C#

Samual Sam
Updated on 22-Jun-2020 14:56:09

137 Views

With C#, you can manipulate decimals with operators such as _+, - *, etc.Let us see how to subtract decimal values.Firstly, set two decimal values −decimal d1 = 9.5M; decimal d2 = 4.2M;Now to subtract the two values −d1 = d1 - d2;The following is the code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       decimal d1 = 9.5M;       decimal d2 = 4.2M;       d1 = d1 + d2;       Console.WriteLine("Addition of Decimals: "+d1);       d1 = d1 - d2;       Console.WriteLine("Subtraction of Decimals: "+d1);    } }OutputAddition of Decimals: 13.7 Subtraction of Decimals: 9.5

Decimal constants in C#

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

2K+ Views

Decimal type has constants to get the minimum and maximum values.Set a decimal value −decimal d = 5.8M;To get the minimum and maximum values of the decimal type, use the following properties −decimal.MaxValue decimal.MinValueHere is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       decimal d = 5.8M;       Console.WriteLine(d);       Console.WriteLine("Maximum Value: "+decimal.MaxValue);       Console.WriteLine("Maximum Value: "+decimal.MinValue);    } }Output5.8 Maximum Value: 79228162514264337593543950335 Maximum Value: -79228162514264337593543950335

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

606 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

Advertisements