-3

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.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Joseph Kim
  • 61
  • 2
  • 3
  • 5

4 Answers4

1

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)
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • @HansPassant:- I think OP has missed to specify the significant number of digits which he wants or may be MidpointRounding.AwayFromZero. But it is just a guess. :) – Rahul Tripathi Dec 03 '13 at 18:41
1

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.

drew_w
  • 10,320
  • 4
  • 28
  • 49
1

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahil
  • 58
  • 4
0

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

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105