- **C#**
(*http://www.go4expert.com/articles/c-sharp-tutorials/*)

- - **Working with Numbers in C#**
(*http://www.go4expert.com/articles/c-sharp-t30022/*)

Working with Numbers in C#In addition to string, another extremely important entity that lays at the foundation of any software application is the numbers. Numbers play a very crucial role, particularly in the business logic layer of any software application. Logic building requires strong knowledge of numbers and their usage. For instance, if you want to fetch the records of all the employs from a database where salary is greater than Rs. 20,000, you must know how to use numbers; how to do number conversions and what mathematical formulas to use in order to achieve the desired functionality such as squaring the terms, getting square roots etc. Fortunately, enough .NET Framework provides multiple numeric types and the classes that can be used for manipulating those numeric types. In this article, we will discuss some of the most important types associated with numbers.
- Static Math Class
- Using BigInteger struct to store large integer values
- Using Complex struct
- Random Number Generation
## Static Math ClassThe first and foremost class that you should have complete understanding of, is the static Math class that provides several built-in mathematical functions that can be used to manipulate numbers. Several mathematical constants, such as E(e), and PI are also defined in the mathematics class. Following are some of the most widely used methods of the static mathematics class. **Round**- This method is simply used to round off a number. For instance, if you pass a number 10.4 as argument to this method, it will round off it and return 10.**Truncate**- This method is used to truncate the decimal part of a number and returns the integer part. For instance if you pass 10.4 to this method, it will truncate .4 and will return an integer 10.**Floor**- The floor method also rounds number off but it rounds off the number down to the nearest integer, for instance if you pass it a number 10.4 it will return an integer 10; if you pass it the number 10.9, it will still return number 10 because it has to round number off to the nearest lower integer which is 10 in both cases.**Ceiling**- This function does exactly the reverse of the Floor method; it takes a number and round it up to the nearest integer. For instance, if you pass 10.4 to this method it will round it to 11. Floor and Ceiling functions are self-explanatory; floor means the floor used in sense lower limit and the ceiling is the upper limit of a room. This is a good way to remember these functions.**Max/Min**- These methods take two values and return the maximum or minimum of those two values. By default, these methods take two numeric values, however if you want to find the maximum or minimum values of a sequence of number or an array, you can use the extensions of these two methods which are available in System.Linq.Enumerable.**Pow**- This method takes two parameters, the first parameter is the number which you want to raise to a power and the second number is the power. For instance, if you pass two numbers such as Math.Pow(4, 2). It means that we want to raise 4 to the power 2, which will return 16.**Log and Log10**- These two methods are used to calculate logarithm of number. Simple Log function is used to calculate natural log to the base (e) of specified number whereas Log10 returns the log of a number to base 10.
Trigonometric Functions - Following are the trigonometric functions available in static Math class.- Sin
- Cos
- Tan
- Sinh
- Cosh
- Tanh
- Asin
- Acos
- Atan
Example1Code:
`using System;` Output1http://imgs.g4estatic.com/c-sharp/numbers/output1.png ## Using BigInteger struct to store large integer valuesA simple integer cannot store very large values, for example if you want to store a number like 10000000000000000000000000000000000000000000000 i.e. a number with 1 followed by 100 zeros or the number of order 10 raise to power 100. If you try to store such number, the compiler might through an exception or even if it doesn’t throw an exception, it will result in loss of precision and the integer might not contain the complete value. For this reason, from .NET Framework 4.0, a new numeric struct has been introduced in the .NET Framework which allows us to store large integer value. BigInteger is the struct that allows us to store large integer values in a struct. By default, C# haven’t got any literal to represent a large value to store in the BigInteger struct however you can store an integer value to BigInteger without having implicitly parse it. For instance you can do BigInteger num = 25 // Here 25 is an integer literal being stored in the BigInteger struct. In order to declare a number of order 10 raise to the power 10, you have to methods. You can either use the Pow static method of the Math class, or you can achieve this functionality via parsing like this Parse("1".PadRight(100, '0')). Another very interesting to note here is that you can explicitly convert BigInteger to double and int values and vice versa but that may result in the loss precision. Now as usual, we will implement BigInteger struct in our example to further elaborate the concept. Have a look at Example2. Example2Code:
`using System;` Output2 http://imgs.g4estatic.com/c-sharp/numbers/output2.png BigInteger struct overloads almost of the arithmetic operators that operate on numeric types including the comparison operator and the % operator for remainder calculation. ## Using Complex structAnother very useful struct that was introduced with .NET Framework 4.0 was the complex struct which is used for storing complex numbers. Complex struct also belong to the same System.Numerics namespace like BigInteger. Complex struct can store a complex number which has two parts i.e a real part and an imaginary part. In order to declare a complex struct you simply have to write this line of code Code:
`Complex num1 = new Complex(5, 1.5);` Complex num3 = 25; Our next example throws light on this concept. Example3, implements the aforementioned concepts. Have a look at it. Example3Code:
`using System;` Output3http://imgs.g4estatic.com/c-sharp/numbers/output3.png ## Random Number GenerationIn the last section of the article, we will introduce you to the random class that is used to instantiate random numbers. Instantiating random numbers is an extremely simple task. You have to create an object of random class like this Code:
`Random num1 = new Random();` However, you can also specify the limit in which the number should be generated. For this purpose, the Next method has two overloads: `Next(int maxvalue)` - Here you specify maximum limit for the number to be generated randomly, for example if you specify 100 in the parameter, the random number generated will not be greater than 100.`Next(int minval, int maxval)` - Here in this method the first parameter is the minimum limit and the second parameter is the maximum limit of the number to be generated.
Example4Code:
`using System;` Output4http://imgs.g4estatic.com/c-sharp/numbers/output4.png |

All times are GMT +5.5. The time now is 06:08. |