How to store currency values accurately 
Author Message
 How to store currency values accurately

I have a financial application which needs to work accurately with currency
data. I know that the float data type is not too accurate, so I assume
people stay clear of this approach. Another way I thought of was to use
long integers to store the values as cents, but I have a problem with this.
My application should allow for values up to $99,999,999.99, but the long
integer on my system is 4 bytes, so the largest number is 4,294,967,295
(when used to store cents this is a maximum of $42,949,672.95). Do people
solve the problem by creating a structure with separate fields for dollars
& cents?

Thanks in advance

Gary Robertson
Crossland Tech
Brisbane, Australia



Sat, 08 Jan 2000 03:00:00 GMT  
 How to store currency values accurately

Quote:

>I have a financial application which needs to work accurately with currency
>data. I know that the float data type is not too accurate, so I assume
>people stay clear of this approach.

However, there are double and long double. The mantissa of those types
provides more bits of minimum storage than a minimum long int.

Quote:
>Another way I thought of was to use
>long integers to store the values as cents, but I have a problem with this.

Storing "cents" (or whatever your currency's smallest unit is) is the
key concept. If you use that, storing them in a double (or, if your
implementation provides more digits in the mantissa of a long double,
in a long double) will work fine for most financial applications.

Quote:
>My application should allow for values up to $99,999,999.99, but the long
>integer on my system is 4 bytes, so the largest number is 4,294,967,295
>(when used to store cents this is a maximum of $42,949,672.95). Do people
>solve the problem by creating a structure with separate fields for dollars
>& cents?

Some do, some use BCD numbers, and some use floating point data types
to store the smallest currency unit.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Sun, 09 Jan 2000 03:00:00 GMT  
 How to store currency values accurately

Quote:

> I have a financial application which needs to work accurately with currency
> data. I know that the float data type is not too accurate, so I assume
> people stay clear of this approach. Another way I thought of was to use
> long integers to store the values as cents, but I have a problem with this.
> My application should allow for values up to $99,999,999.99, but the long
> integer on my system is 4 bytes, so the largest number is 4,294,967,295
> (when used to store cents this is a maximum of $42,949,672.95). Do people
> solve the problem by creating a structure with separate fields for dollars
> & cents?

Hi Gary Robertson,

Using a structure is probably the best way to solve the problem. You
might even succeed to do it portably, by including enough fields in the
structure. The portability problem here is that the C standard does not
garanty that an integer has 32 bits. On some compiler it has less and on
others maybe even more bits. For instance I have 2 compilers with 16 bit
integers right here.

Implementing a large numbers in a portable way is a bit complex and
requires not only a special data type but also a full set of mathematic
functions. There are several such libraries, both free and commercial.
A free implementation of arbitrarely large numbers is in the SNIPPETS
source code collection, it's called BIGNUM. You can get SNIPPETS at:
      ftp://snippets.org/pub/snippets/snip9707.zip & snip9707.tar.gz
      http://www.snippets.org/
      juge.com:/c/snip9707.lzh
      PDN nodes (SNIP9707.RAR) and SimTel mirror sites

Another easy to use but compiler specific solution might be to use
"double" instead of "long". Check your compiler documentation for the
number of significant decimal digits in the mantissa. On compilers
with 8 byte doubles this is usually 15, which solves your problem.

And if we start considering compiler specific solutions, there are
compilers which impement 64 bit integer data types. You might look
for such a compiler.

The least that you can do for the sake of portability is check the
standard C value range constants at the start of your program to
make sure that the compiler used for compiling the program supports
the value range that you expect and require.

Stephan
(initiator of the campaign against grumpiness in c.l.c)



Sun, 09 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. How to format a currency value of zero as blank

2. Add to Value Stored In Separate Variables

3. problem of using SqlDataReader to get output value of a stored procedure

4. Problems stored values in variable

5. String values returned from OracleParameter and Stored Procedure

6. Getting Return value from Stored Proc

7. Getting return value from stored proc.

8. How are floating values stored?

9. Passing NULL values to a stored procedure????

10. store ascii equivalent decimal value into array!!!!

11. Reading a value from a stored proc

12. Storing hex value in char

 

 
Powered by phpBB® Forum Software