If I want to round 3.32 to 3.30 and 3.38 to 3.40, how can I do that?
I tried math.round()
, but I couldn't do that.
If I want to round 3.32 to 3.30 and 3.38 to 3.40, how can I do that?
I tried math.round()
, but I couldn't do that.
You are probably looking for Math.Round Method in VB.NET
Rounds a value to the nearest integer or to the specified number of fractional digits.
Try like this:
Math.Round(3.32, 1)
or this:
Math.Round(3.32, 1, MidpointRounding.AwayFromZero)
Math.Round(3.38, 1, MidpointRounding.AwayFromZero)
You can specify the number of significant figures in the Math.Round routine (overload). I'm used to C# but the VB.NET syntax should be something like:
Math.Round(3.44, 1)
See "http://msdn.microsoft.com/en-us/library/aa340228(v=vs.71).aspx" for more information.
Adding to the previous solutions, to get two-digit correct decimal values, use this:
FormatNumber((Math.Round(3.32, 1, MidpointRounding.AwayFromZero)), 2)
' Returns 3.30
FormatNumber((Math.Round(3.38, 1, MidpointRounding.AwayFromZero)), 2)
' Returns 3.40
Like this:
Math.Round(3.32, 1, MidpointRounding.AwayFromZero) ' Returns 3.3
Math.Round(3.38, 1, MidpointRounding.AwayFromZero) ' Returns 3.4
The first parameter is the number to round. The second parameter specifies how many digits to round to after the decimal point. The third parameter specifies that you want to use standard away-from-zero rounding rather than bankers rounding).