Altering decimal places of float values read from Binary file

This is a discussion on Altering decimal places of float values read from Binary file within the C Programming forums, part of the General Programming Boards category; I am reading a binary file containing records of information. Part of the record is a Account Balance which is ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    6

    Angry Altering decimal places of float values read from Binary file

    I am reading a binary file containing records of information. Part of the record is a Account Balance which is a float value. It is already stored with the default 6 decimal places.

    Does anyone know how I can alter the stored value to only be 2 decimal places. As I am trying to total up all the balances, and the 6 decimals places is altering my final figure.

    This is the line I have that reads the balance from the binary file, but from this point it is stored in the structure member as a 6 decimal place float value.

    Code:
     /* 
    fread(&customer.cust_bal, sizeof(customer.cust_bal), 1, custmast_ptr); */
    Any ideas??
    Kelly

  2. #2
    Unregistered
    Guest
    round the float value to 2 decimals before use :
    Code:
    #include <math.h>
    float round(float f)
    {
     return floor(f+0.5);
    }
    
    ...
    
    float amount = 1.23456;
    amount = round(amount*100)/100;
    printf ("1.23456 rounded to 2 decimals is : %f", amount);
    
    ...

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    I think your are having trouble formatting your output. So try:
    Code:
    printf(" %3.2f\n", f);    /*   f is a float variable   */
    And yes, your fread() has got nothing to do with your 'final figure'. Even if you store the value 1.200000 as 1.2 in your variable, , internally, it wil still be stored as 1.200000

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    6

    Smile

    no, it wasnt the output that was the problem, it was when the data was read from the binary file into the float variable.

    Thanks to everyone, I seem to have (fingers crossed) sorted it by redefining the FLT_DIG constant changing the default number of decimal places floats are stored from 6 to 2.

    seems to be OK so far

    Kelly :-)

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Be very careful when you modify the standard header files for your compiler. Bad things(tm) could happen.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    6

    Angry

    Thanks

    It turned out that actually that hadnt sorted out the problem!

    so im back to square one!

    Basically for the first record it is reading in -2407.629,
    I want to STORE the value as -2407.63

    Anyone have any idea of any functions that will round up/ round down as necessary?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > It is already stored with the default 6 decimal places
    No it isn't
    You've written out the 32 bits which comprise the floating point number, and these have a precision of 6 decimal digits.
    This means that
    0.123456 is accurately stored
    123.456 is accurately stored
    but
    123.456789 is rather more random in the last 3 decimal digits.

    There is nothing you can do to a float which would make it behave as you would want.

    If you really want 2 decimal places, then I suggest you multiply your float by 100, cast the result to an integer, and perform the summation using integers. At the end, divide the result by 100 to get your sum back into floating point format.

    But if all you want to do is print it, try
    printf( "%d.%02d", sum/100, sum % 100 );
    Then you won't have any random noise from the least significant bits of your float.

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi, I think I know the program your working on...424project by any chance...sent pm.. mail me if you require help.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    6
    well I've tried conversion as well now, and that still doesnt work. Perhaps I can better illustrate this way.

    Here is the first 5 float values, and the increasing total balance as they are added.

    -2407.62915 -> -2407.62915
    99173.45313 -> 96765.82398
    988.530029 -> 97754.354
    700 -> 98454.354
    -4729.879883 -> 93724.47412

    However when I add the float values I am getting different total values from the first addition ( done simply by total += value);

    -2407.62915 -> -2407.62915
    99173.45313 -> 96765.82031
    988.530029 -> 97754.35156
    700 -> 98454.35156
    -4729.879883 -> 93724.46875

    Hope I've explained the problem a little better this time. Please please please, any ideas on what the problem is !

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Are you by any chance trying to accumulate the total customer balance of all customers on the new cust mast file in prog 4 424C.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    6
    have just pm'd you bigtamscot. Email me directly if you can help at all to the email address in the message

    THANK YOU!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Opengl walking leg animation
    By Bobby230 in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 02:41 PM
  4. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21