Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numbers... 1

Status
Not open for further replies.

CCortez

Programmer
Sep 18, 2000
16
0
0
BR
VB rounds 8.3 to 8 and 8.8 to 9 (for example).
I'd like to round the number 8.3 to 9 (up) and 8.8 to 8 (down)
Somebody may help me ?
Thank you.
 
You have to work around that. f.e. make a string of the double with str(x) with x as double. Then find the string behind the decimal point. Test that number and do whatever you like, f.e. if number<5 (when using 1 decimal) then x=x+1 and use round(x). 8.3 will become 9.
To my knowledge there is no function in VB that supports this.
 
Try:

RndDown = Abs(Int(-1 * MyVal)

It is really to trivial to put in function.



MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
Or, just add 0.5 and then round the number.

RndVal = Int(Val + 0.5)

Val Adj Int()
1.00 1.50 1
1.01 1.51 2
1.50 2.00 2
1.51 2.01 2
1.99 2.49 2

 
I think a better description would be: &quot;ReverseRounding&quot;. Where Round would go UP, he wants to go DOWN.
Where Round would go DOWN, he wants to go UP.

MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
mea culpa. Dyslexia by syllables!

RndDown = Abs(Int(-1 * MyVal))

Should be

RndDown = Int(Abs(-1 * MyVal))

but even this may not be the whole soloution. What 'results' are expected for negative values?

MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
How about this:

Int(X) + Round(1-(Round(X)-Int(X)))

One problem in that this uses scientific rounding. IE Round(8.5) = 8 (using scientific rounding, rounding to the nearest even number with the case of .5). But when I were a lad, I was taught (and I used to teach) that 8.5 rounded to 9.
Using the above, 8.5 will round to 9, but I think it should round to 8 the way you want it to work, from what I was taught.

Simon
 
2.0 rounds up to 3 (just like 2.1).:cool:
I'm just wondering in what situation would you round this way.:)

David Paulson
 
A minor addition to Simon's code seems to do the trick:[tt]

x = (Int(x) + Round(1 - (Round(x) - Int(x)))) - (1 - (Round((x - Fix(x)) / (x - Fix(x) + 0.00000000001)) And 1))
[/tt]

2 = 2
2.1 = 3
2.9 = 2

Unfortunately, 2.5 = 3 and I think CCortez intended to round down there.

David, I could be wrong but I can only think of one possible application here: Homework Assignment.

Interesting problem, though, don't you think?
VCA.gif

Alt255@Vorpalcom.Intranets.com
 
I don't understand what point David is trying to make:

CCortez wanted to do the opposite of the normal rounding, so normally 2.0 would &quot;round down&quot; to 2 (even though the scalar value of the numbers 2.0 and 2 are the same it is said to round), so I reversed this to round up to 3

Simon
 
I just took it as if you are rounding to a whole number, a whole number would not be rounded. I guess it is a matter of interpetation.
David Paulson


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top