nara son
nara son

Reputation: 39

How to use Math.Round()?

Today I wonder what Math method is used in vb.net's Math.Round(). I have code below and I don't know why the program gives the same result. Can anyone explain this to me?

 Module Module1    
    Sub Main()
        Console.WriteLine("Math.Round" & Math.Round(23.5))
        Console.WriteLine("Math.Round" & Math.Round(24.5))
        Console.ReadLine()    
    End Sub    
 End Module

Upvotes: 1

Views: 992

Answers (3)

Kalu Singh Rao
Kalu Singh Rao

Reputation: 1697

You can use Math.Round for any value. Here example is only for one values

Dim Value1 as string = Math.Round(Convert.ToDecimal(24.51), 2)
Console.WriteLine("Math.Round" & Value1)

Result: 25.00

Upvotes: 0

Luminous
Luminous

Reputation: 1909

As the MSDN explains the Math.Round Method:

Return Value Type: System.Decimal

The integer nearest parameter d. If the fractional component of d is halfway between two integers, one of which is even and the other odd, the even number is returned. Note that this method returns a Decimal instead of an integral type.

Upvotes: 0

Sateesh Pagolu
Sateesh Pagolu

Reputation: 9606

Midpoint values are rounded to the nearest even number.

Change 24.5 to 24.51

    Console.WriteLine("Math.Round" & Math.Round(23.5))
    Console.WriteLine("Math.Round" & Math.Round(24.51))

and see the difference in results

Math.Round24

Math.Round25

Upvotes: 2

Related Questions