[Gambas-user] Currency and addition/subtraction
charlesg
charles at ...1784...
Fri Mar 12 10:14:39 CET 2010
Hi,
I guess the simple answer is:
fTotal += Val(tblView[row, column].text)
However, you will eventually run into rounding errors.
there was quite a lengthy thread not long ago about this. The conclusion (if
such a thing can be reached on a forum :-)) was that currency should be
worked on as integers and decimal points added at display time. The problem
is compounded by barmy tax authorities on the continent who demand
calculations to four decimals.
At the time, I wrote the following simply as a test. As with most of my
programming, it is unlikely to be the most efficient method. :blush:
> PUBLIC SUB btnRun_Click()
> DIM iInt AS Integer
> DIM j AS Integer
>
> IF InStr(txtInput.text, ".") = 0 THEN 'no decimal entered ie assume in
> cents
> TRY iInt = Val(txtInput.text)
> IF ERROR THEN
> PRINT "trapped" 'do someting if it is not a number
> ENDIF
> ELSE
> iInt = Val(txtInput.text) * 10 ^ Val(txtDecPlaces.text) + 0.5 'multiply
> out
> ENDIF
>
> txtInteger.text = Str(iInt) 'display integer representation
>
> IF Len(txtInteger.text) > Val(txtDecPlaces.text) THEN 'if the integer
> equivalent is long enough to accomodate the required dec places
> lblOutput.text = Str(Left(txtInteger.text, Len(txtInteger.text) -
> Val(txtDecPlaces.text))) & "." & Str(Right(txtInteger.text,
> Val(txtDecPlaces.text)))
> ELSE 'pad out with leading zeroes
> lblOutput.text = Str(txtInteger.text)
> FOR j = Len(txtInteger.text) TO Val(txtDecPlaces.text) - 1
> lblOutput.text = "0" & lblOutput.Text
> NEXT
> lblOutput.text = "0." & lblOutput.text
> ENDIF
> END
>
rgds
--
View this message in context: http://old.nabble.com/Currency-and-addition-subtraction-tp27872220p27874102.html
Sent from the gambas-user mailing list archive at Nabble.com.
More information about the User
mailing list