Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Tontow on June 21, 2005, 07:40 PM

Title: always rounding up or down
Post by: Tontow on June 21, 2005, 07:40 PM
Round(expression [,numdecimalplaces])
  Rounds the number, but you don't have any control over wich direction it rounds.  So;
What is the best method to always round a decimal number up?
What is the best method to always round a decimal number down?

(edit; ment round a decimal number eather up or down to a whole number)
Title: Re: always rounding up or down
Post by: hismajesty on June 21, 2005, 08:29 PM
I don't know if there is a built in thing for this but you could just split the number and if the first number after the decimal is >= 5 then round up the number on the left, else round it down.
Title: Re: always rounding up or down
Post by: The-FooL on June 21, 2005, 09:15 PM
Use the least integer function.
Title: Re: always rounding up or down
Post by: Forged on June 21, 2005, 11:04 PM
Quote from: Tontow on June 21, 2005, 07:40 PM
Round(expression [,numdecimalplaces])
  Rounds the number, but you don't have any control over wich direction it rounds.  So;
What is the best method to always round a decimal number up?
What is the best method to always round a decimal number down?

(edit; ment round a decimal number eather up or down to a whole number)

Add one or subtract one from the number and then make the value variable an integer.
Title: Re: always rounding up or down
Post by: hismajesty on June 22, 2005, 08:16 AM
It doesn't always round down. It rounds to nearest even number, Round() utilizes 'Banker's Rounding.'

Here, I found this (http://support.microsoft.com/default.aspx?scid=kb;en-us;196652) for you.

Edit:

Arithmetic Rounding:


Function RoundIt(f As Double, DecPlaces As Integer) As String
If DecPlaces <= 0 Then
f = Int(f * (10 ^ DecPlaces) + 0.5)
f = f / (10 ^ DecPlaces)
RoundIt = f
Else
RoundIt = Format(f, "#." & Left("00000000000", DecPlaces))
End If
End Function


That's from here (http://thedailywtf.com/forums/35456/ShowPost.aspx) which also has some other examples (including an oh so efficient 100 line one :P)
Title: Re: always rounding up or down
Post by: K on June 22, 2005, 08:04 PM
How about this:


Dim foo as Double

' Floor:
Integer I = Int(foo)

' Ceiling:
Integer I = IIf( Int(foo) = foo, foo, Int(foo) + 1)

Title: Re: always rounding up or down
Post by: Topaz on June 23, 2005, 05:29 AM
Rounding is for fat people who try to lose weight
Title: Re: always rounding up or down
Post by: Spilled on June 23, 2005, 12:40 PM
Quote from: Topaz on June 23, 2005, 05:29 AM
Rounding is for fat people who try to lose weight
Off topic... but interesting  :)
Title: Re: always rounding up or down
Post by: Tontow on June 23, 2005, 02:30 PM
Quote from: Topaz on June 23, 2005, 05:29 AM
Rounding is for fat people who try to lose weight

So your saying that there will never ever be a situation where you need to round a number in order to avoid an error? lol