Rounding problem in VB and VB.Net

Posted on 2003-03-06
Medium Priority
Last Modified: 2008-03-17
In VB or VB.Net
round(3.44,1) = 3.4
round(3.45,1) = 3.4
round(3.46,1) = 3.5

Is that any rounding function that gives:
Question by:lippo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 44

Expert Comment

ID: 8087753
Round uses what is generally know as "Banker's Rounding" - which means that when rounding is applied, if the RIGHTMOST digit is EXACTLY 5, then the rounding is such that the Rounded Value ends with an EVEN value:

3.45 (Right-most digit is 5) rounds to 3.4 (since 4 is even)
3.55 would round to 3.6 (not 3.5)


Expert Comment

ID: 8088012
Simple hack for what you want to do:

Round(myNum + .00000000001, 1)

If you are rounding to 1 decimal place, a number that small will never change your data.  It will change the rounding though.

round(3.4400000000001, 1) = 3.4
round(3.4500000000001, 1) = 3.5
round(3.4600000000001, 1) = 3.5

LVL 28

Accepted Solution

iboutchkine earned 150 total points
ID: 8088903
You can use these 2 function

1.Round down

' Rounds the  number to specified number of decimal places
' with 0.5 rounded down.
Public Function RoundDown( _
    ByVal numberIn As Double, _
    ByVal roundTo As Integer) _
    As Double
    ' Arguments : numberIn - The number to round
    '             roundTo - The number of digits to round to
    '             (positive for right of decimal, negative for left)
    ' Returns   : The rounded number

    Dim factor As Double
    Dim temp As Double
    Dim rounded As Double

    factor = 10 ^ roundTo

    temp = numberIn * factor
    rounded = CType((CType(temp, Decimal)) / factor, Integer)

    If Decimal.Subtract(CType(numberIn, Decimal), CType(rounded, Decimal)) > 0.5 / factor Then
        rounded = rounded + 1 / factor
    End If

    Return rounded

End Function

2.Round up

' Rounds a number to a specified number of decimal places
' (0.5 is rounded up).
Public Function RoundUp( _
    ByVal numberIn As Double, _
    ByVal decimals As Integer) _
    As Double
    ' Arguments : numberIn - The number to round
    '             decimals - The number of decimal places to round to
    '             (positive for right of decimal, negative for left)
    ' Returns   : The rounded number

    Dim factor As Double = 10 ^ decimals
    Dim temp As Double = CType(numberIn * factor + 0.5, Decimal)

    Return CType(temp / factor, Integer)

End Function

Author Comment

ID: 8100005
I then have to accept that VB does not provide the most basic rounding function

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 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