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

Graeme Geldenhuys graemeg.lists at gmail.com
Thu Mar 8 08:41:35 CET 2012

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.

  - Graeme -

fpGUI - a cross-platform Free Pascal GUI toolkit

More information about the fpc-pascal mailing list