Round a number up or down

benjamin

Member
Joined
Nov 5, 2008
Messages
22
Location
Cook Islands
Programming Experience
5-10
Hi,

Silly question this, but i'm after a function or method to round a number either up or down based on the following:

####.abcdefg.....

if abc <= ##5.. then
rounddown
else
if abc > ##50 then
roundup
endif
endif

result being: #####.##

all the rounding methods i've seen look like they round up (which i'd consider normal) but client has a desire to round anything like 1.295 DOWN
AND 1.2951 UP

hope that helps - your help appreciated.
 
With Math.Round you can specify decimals, for 2 this will give 1.29 and 1.3. Or did you mean both should be 1.29? If so, you can multiply like 100 and Math.Floor/Truncate it, then divide 100 again :)
 
Thanks very much for the reply.

unfortunately i've found that the math.round feature acts as you'd expect industry standards to round (ie .5 rounds up). For some reason the package i'm working with needs it to round down (??).

The midpoint rounding option on the math.round also only allows for rounding either "away from zero" or "to even".

Both of which aren't really what i'm after - although maybe you know a combination of them?

>>Or did you mean both should be 1.29? If so, you can multiply like 100 and Math.Floor/Truncate it, then divide 100 again

Sorry no:

eg:
1.285 should round DOWN to 1.28
1.2851 should round UP to 1.29

Not sure if i can work out how this would fit the .floor and .truncate methods?

Appreciate your help.
 
1.285 should round DOWN to 1.28
1.2851 should round UP to 1.29
Those are different numbers than you posted first, and the results you ask for is default rounding, Math.Round them to 2 decimals and you get the results you ask for.
 
Thanks John,

I was about to kick myself stupidity (which i may still do) as you're correct.
I changed the values to provide a better example for you and yes those values parsed through math.round are correct.

For some reason the bug which brought me to this is with 1.495 (which by the rules i mentioned should round down):
Here's the output i find inconsistent:

immediate window:

? math.Round (1.495,2)
1.5 ---> this should be 1.49
? math.Round (1.285,2)
1.28 ---> this is what i want

yet they aren't using the same rules by the looks?

So, i can't see whats wrong here - the 1.495 rounded to two decimals should (if its using the same method as 1.285), round to 1.49!

Now i'm really confused - are you able to duplicate using 1.495 as your rounding base?


Those are different numbers than you posted first, and the results you ask for is default rounding, Math.Round them to 2 decimals and you get the results you ask for.
 
Ok i think i know the reason for the inconsistent:

? math.Round(1.485,2)
1.48
? math.Round(1.495,2)
1.5

"The number nearest d with precision equal to decimals. If d is halfway
between two numbers, one of which is even and the other odd, then the even
number is returned. If the precision of d is less than decimals, then d is
returned unchanged."

Math.Round not working for a certain number...

Which is a problem for me - do you know if there is another way around this other than breaking the string down and doing some funky string ops?
 
Completely agree with you. Unfortunately the unfair package that i need to match my values with is unfair.

which is completely unfair in my eyes.

Do you know of an alternative solution?

Thanks.
 
You could simply do this :

VB.NET:
-Math.Round(-value)

EDIT : Hmmm... Actually not :(

I actually learned something about rounding negative numbers today... How about this?

VB.NET:
if Math.Abs(value - Math.Round(value)) < 0.5 then
    return Math.Round(value) 
elseif value > 0 then
    return Math.Floor(value) 
else
    return Math.Ceiling(value)
end if

You'll have to multiply/divide the value by 10^X where X is the number of digits you need. Just divide the value before the calculation and multiply after...

EDIT2 : Now I'm thinking "value - Math.Round(value)" might not always be equal to 0.5 for floating point numbers even if the logical value should be... :eek:

I guess you only true solution is to translate to a String and work from there...
 
Last edited:
All must hail!

This is excellent - although i know the rounding is not ideal, this solution is a fantastic work around.

Thanks so much - this works a treat with such a small number of lines - i've never actually seen the floor and ceiling methods in use and was becoming annoyed with trying to split string up and manipulate from there.

So, thankyou to all who took the time to consider!

cheers
 
As I edited my post for, it is possible that foating point arythmetic accuracy problems would mess things up. Now I'm not totally sure about "Math.Abs(value - Math.Round(value))" being correctly represented, so the value could fall just a bit under 0.5 when it should fall directly on it and you could get the wrong value.

See this : Floating point - Wikipedia, the free encyclopedia

If you are using Decimal instead of Double ou Float, I think the problem (if there was any) should go away as well.
 
Solution to RoundDown

Place the number to round in TextBox1 and the number of places to round to in TextBox2. Then run this code:

Dim decnum As Decimal, place As Integer, roundown, n As Double
Decimal.TryParse(TextBox1.Text, decnum)
Integer.TryParse(TextBox2.Text, place)
n = decnum * (10 ^ place)
n = Int(n + 0.44444444)
roundown = n / (10 ^ place)
MessageBox.Show("Midpoint rounded down: " & roundown.ToString)
 
Back
Top