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

Int/Fix Bug 2

Status
Not open for further replies.

Skie

Programmer
Jun 21, 2004
475
US
Ok, I'm writing a script that deals with money amounts. But, I want it to simply drop fractions of a cent. So, I wrote the following code:
Code:
Amt = 8.95
WScript.Echo "Amt = " & Amt
Amt = Amt * 100
WScript.Echo "Amt * 100 = " & Amt
Amt = Fix(Amt)
WScript.Echo "Fix(Amt) = " & Amt
Amt = Amt / 100
WScript.Echo "Amt /100 = " & Amt
Amt = FormatNumber(Amt,2)
WScript.Echo "FormatNumber(Amt,2) = " & Amt

Somehow, I lose a cent during Fix (or Int). Now, some interesting bits:
Fix(895) = 895
Fix(8.95 * 100) = 894
Fix(995) = 995
Fix(9.95 * 100) = 994

Any ideas on why it rounds the whole number down? From what I can tell I have a two options, hard code for values like 8.95 and 9.95 or multiply by 100.00000000001. The problem with the first option is, I'd need to find all the times when Fix rounds wrong. The problem with the second is it would round up for million dollar values with .91 fractions of a cent.

So, any work arounds are appreciated.
 
Have you tried the CCur function ?
Amt = CCur(8.95)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
CCur doesn't work for fractions of cents though.
8.951 doesn't drop the .001.

I tested the Fix(Amt * 100) for all values between .01 to $10.00 (ignoring fractions of cents) and it drops a cent 1000 times.
 
Oops, bad use of a counter. If it was 1,000, that'd be everytime. It happens 69 times.
 
I suggested this:
Amt = CCur(8.951)
Amt = Amt * 100
Amt = Fix(Amt)
Amt = Amt / 100
WScript.Echo "Amt /100 = " & Amt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hey, that works. Sorry, was thinking you meant to only use CCur.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top