[fpc-pascal] Currency and ABS(-674.59)

Marcos Douglas md at delfire.net
Thu Mar 8 13:14:58 CET 2012


On Thu, Mar 8, 2012 at 4:41 AM, Graeme Geldenhuys
<graemeg.lists at gmail.com> wrote:
> On 8 March 2012 03:28, Marcos Douglas wrote:
>>
>> Integer math... what did you mean?
>
> Over the years we have experience many problems with currency,
> floating point and date/time calculations and storage. So we tweaked
> our software to handle such data slightly different.
>
> If a user enters and amount of 1500.25, it is immediately converted to
> a Integer as 150025. We have a global project wide setting that set
> the amount of decimals to support - default being 2. So then all
> calculations and comparisons (which is a big issue in floating point
> values) are done with Integer types. It is stored in the database as
> an Integer as well. But when it is displayed anywhere to the user, it
> is reformatted with decimals (for the display only).
>
> Similar thing with dates. We always store it as a string using the ISO
> 8601  [ http://www.cl.cam.ac.uk/~mgk25/iso-time.html ] format. This
> way, no matter the database being used (MS SQL Server, Firebird,
> Oracle etc.) with their various timestamp field types, or what locale
> the database server used for that specific database (which affects the
> timestamp field type storage format), we always know the format the
> date and time is stored in. When the data is loaded we have a
> ISO_to_DateTime() routine (and the reverse when stored) that converts
> it to a TDateTime in memory. Displaying of the date/time to the end
> user can be configure by the end-user (or application or system wide).
> Because the date/time is always in yyyymmddThhmmss format, even SQL
> working directly with the date/time fields can still do comparisons
> like is one date larger that the other.
>
> Since we have used these two methods (for the past 5 years) in our
> projects, we have just about eliminated all issues we previous had
> with currency, float and timestamp data.
>
>
> And as was mentioned, rounding and comparisons are major issues with
> floating point data, and non-existent in Integer data.

So, if you have two values to storage (amount of decimals is 4) e.g.
100.34524 and 2,000.2 you do:
100.34524 = 1003452
2,000.2 = 20002000

I never heard about this technique before. Very good!

For DateTime I already do this, but not in all cases.

Marcos Douglas



More information about the fpc-pascal mailing list