Money or float
Just a quiz thread,
I have a table with a "from date", a "to date", and a total interest amount. I need to work out, on a simple interest basis, the proportion of interest applicable to a subset of days within this range. Basically I'm using datediff to get the number of days between the two, then calculating the number of days from my own starting date to the to date and dividing this by the total to get the fraction to multiply by the interest applied, or Interest = P * R * T. Once customer cashes out I need to store the interest earned and keep track of all interest paid out.....
So whilst on business layer I'll only show the total paid to customer up to ,2 digit precision
What data type would I store this as?
I have a table with a "from date", a "to date", and a total interest amount. I need to work out, on a simple interest basis, the proportion of interest applicable to a subset of days within this range. Basically I'm using datediff to get the number of days between the two, then calculating the number of days from my own starting date to the to date and dividing this by the total to get the fraction to multiply by the interest applied, or Interest = P * R * T. Once customer cashes out I need to store the interest earned and keep track of all interest paid out.....
So whilst on business layer I'll only show the total paid to customer up to ,2 digit precision
What data type would I store this as?
Well, this all depends! But I ACTUALLY had to do this on my first real IT job. It was as a contract job for a bank, and they did NOT want to be off by more than a penny, and this was on THOUSANDS of accounts. The number of places I was asked to have, and did, was FIVE to the right of the decimal point. The actual number printed was a CURRENCY format, with TWO digits to the right, but internally it tracked the whole value. SO, say the offset was .00812,
The next amount could end up being 1987.13100. It would print as 1987.13. If the number after THAT were 1987.13100, then IT would print as 1987.14. The offset would end up being .00012.
But this shows how you would NEVER end up even as much as a penny off. Ironically LATER, they had that movie office space which was about some guys that HATED where they worked, had the same sort of calculation method, and tried to store the offset in an account THEY had! THEY made a mistake and ended up with a HUGE amount, etc....
Personally, I'd represent them in something along the line of hundredths or thousandths of a penny and use an int or a long or whatever equivalent the language has. As long as the amount rounds to a penny, you're likely fine. Situations like Super Man II and Office Space with people shaving fractions of a cent mostly don't happen because the amount of money that is being paid in interest to customers should always be less than the amount being received for the investment by enough not to have to worry about a fraction of a penny. If that money can't be transferred, then folks typically won't try to save that fraction of a penny. The issues of fraud would be addressed separately from that in terms of internal controls.
I do not believe there is a standard number of decimal places to go and you'll wind up rounding it all to the penny eventually.
Similar Topics | |
---|---|
Is money really everything? |
22 Dec 2024, 1:18 pm |
Transferring Money |
03 Jan 2025, 2:09 am |
Lionel Messi got lucky or is it because of the money? |
12 Dec 2024, 6:30 am |
Happiness: Why Purpose Beats Money Every Time |
23 Jan 2025, 9:19 pm |