Valhalla Legends Forums Archive | Visual Basic Programming | always rounding up or down

AuthorMessageTime
Tontow
[code]Round(expression [,numdecimalplaces])[/code]
  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)
June 22, 2005, 12:40 AM
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.
June 22, 2005, 1:29 AM
The-FooL
Use the least integer function.
June 22, 2005, 2:15 AM
Forged
[quote author=Tontow link=topic=11917.msg116797#msg116797 date=1119400830]
[code]Round(expression [,numdecimalplaces])[/code]
  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)
[/quote]

Add one or subtract one from the number and then make the value variable an integer.
June 22, 2005, 4:04 AM
hismajesty
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:

[code]
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
[/code]

That's from here which also has some other examples (including an oh so efficient 100 line one :P)
June 22, 2005, 1:16 PM
K
How about this:

[code]
Dim foo as Double

' Floor:
Integer I = Int(foo)

' Ceiling:
Integer I = IIf( Int(foo) = foo, foo, Int(foo) + 1)
[/code]
June 23, 2005, 1:04 AM
Topaz
Rounding is for fat people who try to lose weight
June 23, 2005, 10:29 AM
Spilled[DW]
[quote author=Topaz link=topic=11917.msg117008#msg117008 date=1119522598]
Rounding is for fat people who try to lose weight
[/quote]
Off topic... but interesting  :)
June 23, 2005, 5:40 PM
Tontow
[quote author=Topaz link=topic=11917.msg117008#msg117008 date=1119522598]
Rounding is for fat people who try to lose weight
[/quote]

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
June 23, 2005, 7:30 PM

Search