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

Help with INT function

Status
Not open for further replies.

sparrow

Programmer
Jun 14, 1999
35
0
0
HK
I have a field ThisAmount defined as Double
Using the following code:
ThisAmount=4747.69
ThisAmount=ThisAmount * 100
ThisAmount=INT(ThisAmount)
ThisAmount=ThisAmount/100

The INT function converts the value of 474769 to 474768

WHY ?????


Paul McEwan
 
Not for me!!!

If you go into the immediate window (View, Immediate Window) and type:

? Int(474769) and press return the answer should be 474769.

What are you trying to do anyway??
Simon
 
Simon

This is part of a financial program allocationg invoice amounts by percentage.
Because a percenatage of an amount can have many decimal places I am trying to ensure that the value is set to only 2 decimal places.


Paul
 
How about using the FormatCurrency function???:

FormatCurrency(123.4567) = $123.46
Simon
 
Simon

I am using VB5, this function is not available


Paul
 
How about Round(123.4567,2) = $123.46?

VCA.gif

Alt255@Vorpalcom.Intranets.com

"What this country needs is more free speech worth listening to."[tt]
Hansell B. Duckett[/tt]​
 
ThisAmount=ThisAmount * 100

Instead of assigning the result of this operation to your Double variable, try assigning it to a long, or a currency. Like this:
[tt]
Dim ThisAmount as Double
Dim lAmount as Long

ThisAmount=4747.69
lAmount=ThisAmount * 100.0
ThisAmount=lAmount/100.0
[/tt]
I think this will do the truncation properly (and not round up). If this doesn't work, try:
[tt]
ThisAmount=4747.69
ThisAmount=ThisAmount * 100.0
ThisAmount=Int(ThisAmount + 0.5) - 1.0
ThisAmount=ThisAmount/100.0
[/tt]

You should be aware that the division by 100 is also likely to introduce rounding errors, and may negate all your efforts.

Chip H.
 
You most declare As Single

Dim ThisAmount As Single
ThisAmount = 4747.69
ThisAmount = ThisAmount * 100
ThisAmount = Int(ThisAmount)
ThisAmount = ThisAmount / 100

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Eric -

I think Sparrow might be hitting a problem with base-2 rounding. 4747.69 seems to be one of those numbers which doesn't have a precise representation in IEEE floating point format.
When the Int() is called, it makes a rounding decision based on the binary image of the number, and thus rounds down. By assigning it to a Long (or currency) as an intermediate step, you get it out of IEEE format and into a format where 474769 can be represented precisely.

Try running my code snippet, you'll see.

Chip H.
 
Ok Chip i understand now

Round with double :

Function Round(nValue As Double, nDigits As Integer) As Double
Round = Int(nValue * (10 ^ nDigits) + 0.5) / (10 ^ nDigits)
End Function

Private Sub Form_Load()
Dim ThisAmount As Double
ThisAmount = 4747.69
MsgBox Round(ThisAmount, 2)
End Sub

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top