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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1377.png" data-lazy- height="21" src="data:image/svg xml,” width=”359″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1380.png" data-lazy- height="164" src="data:image/svg xml,” width=”523″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1382.png" data-lazy- height="71" src="data:image/svg xml,” width=”373″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1384.png" data-lazy- height="128" src="data:image/svg xml,” width=”528″>

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”.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1386.png" data-lazy- height="72" src="data:image/svg xml,” width=”715″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1388.png" data-lazy- height="128" src="data:image/svg xml,” width=”534″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1390.png" data-lazy- height="54" src="data:image/svg xml,” width=”378″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1392.png" data-lazy- height="165" src="data:image/svg xml,” width=”452″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1393.png" data-lazy- height="271" src="data:image/svg xml,” width=”421″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1394.png" data-lazy- height="126" src="data:image/svg xml,” width=”640″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1395.png" data-lazy- height="70" src="data:image/svg xml,” width=”470″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1396.png" data-lazy- height="181" src="data:image/svg xml,” width=”499″>

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”.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1397.png" data-lazy- height="53" src="data:image/svg xml,” width=”358″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1398.png" data-lazy- height="145" src="data:image/svg xml,” width=”654″>

The yielded result displays that the value of the “x” decimal has been decremented by 1.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1399.png" data-lazy- height="74" src="data:image/svg xml,” width=”374″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1400.png" data-lazy- height="182" src="data:image/svg xml,” width=”682″>

After running this code, we got the value converted to double, and the decimal exponential power for “y” has been round-off.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/word-image-1401.png" data-lazy- height="93" src="data:image/svg xml,” width=”383″>

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.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/05/echo/Author-Image-150×150.jpg6293dcfa644cb.jpg" height="112" src="data:image/svg xml,” width=”112″>

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.