I've always wanted to know:
Is it better to store financila data as a float: eg. 19.99
or as an int: eg 1999
(both values are to represent the same some of money)
Which would be better to keep more acurate information?
I've always wanted to know:
Is it better to store financila data as a float: eg. 19.99
or as an int: eg 1999
(both values are to represent the same some of money)
Which would be better to keep more acurate information?
I've always wondered this too, but I just use float for simplicity and have never encountered any problems.Originally Posted by wintellect
WaltP: why remove the if statement? the program is more efficient without it, there's no point in using a function if you're passing it nothing to process.
AFAIK financial corporations use base-10 floating points, but it's a good habit to use doubles over floats unless you have a specific reason (some floats give truncated results when being passed to an int).
WaltP:
Thanks for the feedback. My original attempt does calculaye the pay correctly, but I can see what you mean about it being slower, so I went back and changed it to this:
what do you think?Code:#include <stdio.h> int main() { float fHours = 0.0; float fPayRate = 0.0; double dTotalPay = 0.0; puts("Please enter number of hours worked:"); scanf("%f", &fHours); puts("Please enter hourly pay rate:"); scanf("%f", &fPayRate); //if overtime, calculate overtime pay if(fHours > 40.0) { dTotalPay = (fHours - 40.0) * (fPayRate * 1.5); fHours = 40.0; } //calculate regular pay, add overtime if applicable dTotalPay += fHours * fPayRate; printf("The Employee earned $%g ", dTotalPay); return 0; }
For the financial data to be accurate, you have to use some type of int (int, long, long long). My guess is you would simply store the amounts in cents. Then to print you'd use (amount / 100) and (amount % 100).Originally Posted by wintellect
A double or float can't represent every decimal number exactly, so if you need 100% accuracy, you must use something else.