Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Rounding problem in VB and VB.Net

Posted on 2003-03-06
4
Medium Priority
?
741 Views
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:
round(3.44,1)=3,4
round(3.45,1)=3,5
round(3.46,1)=3,5
0
Comment
Question by:lippo
4 Comments
 
LVL 44

Expert Comment

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

AW
0
 
LVL 2

Expert Comment

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

HTH
0
 
LVL 28

Accepted Solution

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

Author Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

578 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