• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 893
  • Last Modified:

Number Rounding to the Nearest 0.5

I have several numbers collected from a database that I need to round.

These numbers vary from 2.444 to 2.645 and different again.

I need all number rounding to 0.5 mark, so 1.666 would be 1.5 1.444 would be 1.5 but 1.2333 would be 1.

How can I accomplish this in visual basic 6?
0
Terry Rogers
Asked:
Terry Rogers
  • 4
  • 3
  • 2
1 Solution
 
Jaime OlivaresSoftware ArchitectCommented:
the usual trick is to multiply by 2, round, and divide by 2

0
 
AutoeformsCommented:
i no longer have vb 6 installed but there should be a function called round that will do the trick for you

greg

0
 
Jaime OlivaresSoftware ArchitectCommented:
it should be:

int(yournumber*2)/2
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Terry RogersIT Senior EngineerAuthor Commented:
jaime_olivares:

You solution works, but not as entirely expected. A Figure of  21.77 gets rounded down to 21.5 not to 22 like it should be.
0
 
Jaime OlivaresSoftware ArchitectCommented:
ok, try with:
int((yournumber+0.5)*2)/2
0
 
Terry RogersIT Senior EngineerAuthor Commented:
Now, the number 21.6666666666667 gets rounded to 22 not, 21.5 as it should be. ???
0
 
Jaime OlivaresSoftware ArchitectCommented:
Oh, sorry, is should be:
int((yournumber+0.25)*2)/2

0
 
AutoeformsCommented:
i think you will be more accurate if you search out the vb6 math library.

greg
0
 
Terry RogersIT Senior EngineerAuthor Commented:
Works fine now. Thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now