Solved

How to round a number in vb.net

Posted on 2011-09-20
4
429 Views
Last Modified: 2012-05-12
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
Comment
Question by:Hagita
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
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

by:lludden
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

by:lludden
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

by:x77
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio editor? 1 38
Close word object 13 38
Help with Query not working in client's PC 1 36
C# Single Form 8 29
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now