We often make an operation between several number that makes the result of decimal number, some divided numbers can caused a long decimal number behind “.”, and will cause a non desired output! so to avoid this we need to use Math.Round() method, the mentioned can round double or decimal type in accurate way to it’s nearest value.
//Unarranged Output without Math.Round()!
static void Main(string[]args){
double b = 11;for(int a = 1; a<10;a++){Console.Write(b/a + “\t”);a++;Console.Write(b/a + “\n”);}
}
The result Output Will be…
//Arranged Output with Math.Round()!
static void Main(string[]args){
double b = 11;for(int a = 1; a<10;a++){//Math.Round(Decimal Value, Max Decimal Behind Period(.))Console.Write(Math.Round( b/a, 2) + “\t”);a++;Console.Write(Math.Round( b/a, 2) + “\n”);}
}
And here is the result!
static void Main(string[] args)
{
double a, awayFromZero, toEven;
a = 123.45;
awayToZero = Math.Round(a, 1, MidpointRounding.AwayFromZero);
toEven = Math.Round(a,1,MidpointRounding.ToEven);
Console.WriteLine(a);
Console.WriteLine(awayFromZero);
Console.WriteLine(toEven);