Thread: Looking for a mentor, of sorts

  1. #16
    Registered User wintellect's Avatar
    Join Date
    Mar 2006
    Posts
    24
    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?

  2. #17
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by wintellect
    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.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Location
    Plymouth, U.K.
    Posts
    13
    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).

  4. #19
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    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:
    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;
    }
    what do you think?

  5. #20
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by wintellect
    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?
    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).

    A double or float can't represent every decimal number exactly, so if you need 100% accuracy, you must use something else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  2. Simple Sorts Confuse ME =/
    By otchster in forum C Programming
    Replies: 5
    Last Post: 12-03-2005, 02:02 PM
  3. sorts
    By xddxogm3 in forum C++ Programming
    Replies: 5
    Last Post: 10-17-2004, 07:26 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. sorting of sorts....hehe
    By newbie2C++ in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2001, 01:02 AM