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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rounding numbers

Status
Not open for further replies.

shanley06

Programmer
Nov 26, 2002
101
US
i was wondering if someone could tell me how to round numbers with decimals to a whole number
 
INT() always rounds the number down.
If you want it to round as a normal human would round such as 4.5 to 5 instead of what Qbasic would do and round 4.5 to 4 then just add .5

a=4.5

If you wanted to round like a normal human then you would
a=a+.5
PRINT INT(a)
Answer is 5

Or just round like the computer would
PRINT INT(a)
Answer is 4
 
a = 4.55
IF a > INT(a) + .5 THEN
a = INT(a) + 1
ELSE
a = INT(a)
END IF
PRINT a
 
Since QB4.5 doesnt have a currency floating point type here
is what I did.

DIM Dollar as long
DIM Cents as integer
DIM Amount as double

input amount
Dollar=Amount
Cents=(Amount-Dollar)*100
print Dollar;".";Cents
print using "##,###.##";Dollar+cents/100

this can also be stored in 5 bytes using mkl$ and chr$
amt$=msk$(Dollar)+chr$(cents)

one Caveat: you need to check for negative cents before
using chr$(cents)

If Cents<0 then Dollar=Dollar-1:Cents=Cents+100

then to get them back
Dollar=CVL(mid$(amt$,1,4))
Cents=asc(mid$(amt$,5,1))

 
To round like a human, use the CINT() function. On extremely large numbers, using CINT() will give you an error. That is why most people use the INT() function whenever possible.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top