• Welcome to Valhalla Legends Archive.
 

always rounding up or down

Started by Tontow, June 21, 2005, 07:40 PM

Previous topic - Next topic

Tontow

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)

hismajesty

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.

The-FooL

Use the least integer function.

Forged

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.
QuoteI wish my grass was Goth so it would cut itself

hismajesty

#4
It doesn't always round down. It rounds to nearest even number, Round() utilizes 'Banker's Rounding.'

Here, I found this 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 which also has some other examples (including an oh so efficient 100 line one :P)

K

How about this:


Dim foo as Double

' Floor:
Integer I = Int(foo)

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


Topaz

Rounding is for fat people who try to lose weight

Spilled

Quote from: Topaz on June 23, 2005, 05:29 AM
Rounding is for fat people who try to lose weight
Off topic... but interesting  :)

Tontow

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