Solved

# How to round a number in vb.net

Posted on 2011-09-20
428 Views
Hi Experts,
I need to round numbers up and keep 4 decimal places. as I understand vb does not round up but down.for example the number 0.3456712 needs to be a double type:0.3457
how can I do that please??
0
Question by:Hagita
• 2

LVL 1

Accepted Solution

jdruper earned 63 total points
ID: 36567532
Hi Hagita,

All you need to do is use the Math.Round() method, it will round up or down depending on your number, you can also use its overload to specify how many places you want to round.

For example:

Math.Round(0.3456712 , 4) will return 0.3457

Math.Round(2.34 , 2) will return 2.30

Hope this helps.
0

LVL 18

Assisted Solution

lludden earned 125 total points
ID: 36567541
VB.Net does standard rounding.  If value is .5 or more, it rounds up, else it rounds down

Dim v As Double = 0.3456712
MsgBox(Math.Round(v, 4).ToString)

0

LVL 18

Assisted Solution

lludden earned 125 total points
ID: 36567559
Actually, it is more precise than that.  The standard method for rounding is that if it is >.5 is rounds up, if it is less than .5 it rounds down.  If it is exactly .5, it rounds either up or down, depending on the value of the previous digit - odd rounds up, even rounds down.

So 1.0055 rounded to 3 places is 1.006 and 1.0045 is 1.005
This is the standard way to even out rounding errors.
0

LVL 15

Assisted Solution

x77 earned 62 total points
ID: 36567598
Dim x = Math.Round(1234.545, 2, MidpointRounding.AwayFromZero) ' Result: 1234,55
Dim y = Math.Round(1234.545, 2, MidpointRounding.ToEven)              'Result:1234,54

Use AwayFromZero to minimize error.

Use ToEven where you are using money. Bank round.
0

## Featured Post

Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …