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
Medium Priority
741 Views
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
Question by:lippo

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)

AW
0

LVL 2

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

HTH
0

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
0

Author Comment

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

## Featured Post

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