| by Arround The Web | No comments

## C# Decimal

In mathematics, the decimal value is said to be the floating-point value in which each of its digits can range from 0 to 9. We can call some decimal a proper decimal value with an exact number of digits after the “dot” on the right side of a value. In C#, there is a maximum and minimum value for decimal point values which we will be showing you in the coming examples. So, we have decided to cover some examples for using the decimal values in C# in this article. ## Example 01

We will be starting our first illustration by explaining the difference between the use of decimal and double type variables in C# code. For this, we have to use the C# “System” library, which is a must in C# codes for the use of input and output related function statements. You need to create a namespace having the name “Test” as we did below and create a new class “Test” within it. This class must contain a main() function that is also a driver function for this code.

Right now, there is no need to create extra functions as we can perform most of the tasks in this main() method. We have initialized two double variables, “x” and “y,” with double floating-point values. The two Console.WriteLine() function statements display both the double variables on our shell screen in Ubuntu 20.04 system. Now, the code is sufficient and prepared for use. We have saved this code using the Ctrl+S keystroke. You need the “mcs” compiler for C# to run, compile this code and create its “exe” file. The “exe” file can be initiated in the home folder of your Linux system. You have to use the “exe” file in the “mono” runtime instruction to execute the compiled code as we did below. Both values for variables “x” and “y” are displayed on the screen. Now, it’s time to use the “decimal” data type for a floating-point value in C#. So, we have updated the code by using the initialization of the decimal type floating value for variable “x” in the main() function. After this, we used the Console.WriteLine() function statement to display the decimal type variable “x” floating-point value on the shell. Let’s save our code with Ctrl+S and compile it on the shell via the “mcs” compiler. After compiling this code with the “mcs” C# compiler, we have got the error shown in the image. It tells us that the decimal type variable must contain the value with the suffix “m”. To avoid this error, we need to update the C# code. You only have to put the suffix “m” at the end of a decimal variable value “x,” as shown below. Overall code will be unchanged and used as we did before. Let’s just save this code and run it again to see the changes. After compiling this code on the shell with the “mcs” compiler, we have got to know that the error has been removed after using the suffix “m” at the end of a decimal variable value. After running the “exe” file for this C# code, we have got the decimal floating-point value “x” displayed on our shell screen, i.e., 2.450. ## Example 02

Let’s get started with decimal variables in the C# code to perform different tasks this time. Using the same C# general code structure, i.e., System library, namespace, class, and main() function, we have been initializing a decimal variable “x” with a decimal floating-point value followed by the suffix “m”. We have utilized the while statement to perform some calculations until the value of a decimal variable “x” is not equal to 0. Until the condition is true, it will continue to display the value of “x” by using the Console.WriteLine() function and continue to divide the value of “x” by 100. This loop will continue until the division reaches x=0. After saving our C# code, we accumulated the code with the “mcs” compiler and executed it with C #’s “mono” runtime of C#. The below-shown result is displayed after the while loop is executed from the above code. ## Example 03

Let’s take a look at the example of getting the maximum and minimum decimal values used in the C# code. We have started this example code with the System library, Test namespace, and Test class. We have used the main() function to use two Console within the class.WriteLine() function statements to get the maximum and minimum decimal values separately. We have been utilizing the Decimal class dot product with MaxValue and MinValue objects in the Console.WriteLine() statement. Let’s save this newly created C# code to see how it works and what it returns. So, after compiling this code, we have executed its updated “exe” file with “mcs” and “mono” runtime instructions one after another. We have the long maximum and minimum values displayed on the shell for the decimal object. The maximum value is a very large positive integer, while the minimum value is a very small negative integer, as presented below. ## Example 04

It’s time to perform some mathematical calculations between decimal and integer values to see what it returns. So, within the main() function of this code, we have initialized an integer variable “x” with a value of 45 and a decimal variable “y” with a decimal floating-point value of “0.1445”. After this, we have declared another variable, “sum” of decimal type. We have used the “Add” function of a “Decimal” class from the “System” library to add integer “x” with decimal “y” and save the result into another decimal variable, “sum”. The Console.WriteLine() function displays the calculated sum of both decimal and integer values. After compiling and running this code, we know that the sum has been calculated in decimal floating-point because the return type of variable “sum” is declared as “decimal”. ## Example 05

We can also perform the increment and decrement operations on the decimal values. So, we have initialized a decimal variable “x” and displayed it on the shell with the simple WriteLine() method. After that, we used the pre-decrement operator to decrement the value of “x” and displayed it on the shell again. The yielded result displays that the value of the “x” decimal has been decremented by 1. We can convert a decimal type variable value to a double type variable. For this, we have used the same variable “x” and used the “ToDouble() function of the Decimal class to convert the decimal variable “x” to double. The decimal value “y” is written in exponential form and can also be displayed as the normal value. After running this code, we got the value converted to double, and the decimal exponential power for “y” has been round-off. ## Conclusion

This was all about the decimal values and their uses in the C# example programs. We have discussed simple examples to find out the maximum and minimum decimal values allowed in the C# code for use. Also, we have discussed the ways to initialize and declare it while comparing it with the double-type variables. We have also discussed some very basic C# examples to find out the sum of integer and decimal and convert exponential decimal to a normal integer value.

Source: linuxhint.com