[DUG] Need help in format function
Vikas...
vikas.image at gmail.com
Wed Mar 3 23:23:50 NZDT 2010
Thanks everyone for their prompt reply.
What solution Beven suggested works pretty well when decimal place is 2 and
i appreciate his early response .
How ever we have given option for clients to change decimal places to 2 3 4
5 6, Unfortunatly we forgot to mention to clients these options are for
amounts how ever we made that tab generic for anything related to extended
so can argue with them.
So for time being i did a work arround though it is not at an ideal solution
when he enter say 123.345678
then i took the decimal place
ipos:= pos('.',temp);
value := strtofloat(copy(temp,1,ipos+decimalpace))
this works out for time being as a work arround since my clint cant wait any
long.
However thanks John for the way u told will look in to it.
thanks
On Wed, Mar 3, 2010 at 3:42 PM, John Bird <johnkbird at paradise.net.nz> wrote:
> Tax calculations are tricky, as there are rules about either truncating to
> whole dollars or down to whole cents. These are not maths functions, you
> may have to make your own.
>
> I have seen people do all sorts of tricks with formating floats as strings
> and truncating, and converting back but to my mind the best is to calculate
> in cents - which is what the rules are actually specifying. ie convert the
> dollars to integer or int64 cents, do the calculation and convert the
> result
> back to float. Otherwise you start getting oddities where Net + GST <>
> Gross when the amounts are printed to dollars and cents.
>
> Here is an example with GST calculations - guaranteeing that Net + GST =
> Gross when printed as dollars and cents to 2 decimal places..:
>
> //IRD guidelines are that GST calculated amount is rounded down to nearest
> cent
> procedure xcGSTCalc(aGSTRatePercent:double;var aNetVal:Double;var
> aGSTVal:Double;var aInclVal:Double);
> var
> lGSTRate:int64; //gst rate as cents per 100 dollars (ie per 10000
> cents)=GSTRate*100
> lNetCents,lGSTCents,lInclCents:int64;
> begin
> lGSTRate:=trunc(aGSTRatePercent*100); //eg 1250
> lNetCents:=round(aNetVal*100);
> lGSTCents:=(lNetCents * lGSTRate) div 10000; //1250 div 10000 = .125
> //note integer division
> truncates fractions
> lInclCents:=lNetCents+lGSTCents;
> //return to float - this should guarantee nothing after first 2 decimal
> places
> aNetVal:=lNetCents/100;
> aGSTVal:=lGSTCents/100;
> aInclVal:=lInclCents/100;
> end;
>
> similar rules apply to with tax calculations - there is usually some
> truncating to whole dollars or cents involved.
>
> Incidentally this stuff fools the IRD too - for years the printed tax
> tables
> have had some errors because they didn't follow their own truncating and
> rounding rules for some calculations - and the printed tax tables did not
> agree with their online tax calculator which was correct.
>
> John
>
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject:
> unsubscribe
>
--
vikas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://listserver.123.net.nz/pipermail/delphi/attachments/20100303/f783cf2e/attachment.html
More information about the Delphi
mailing list