how does %.2f work?

This is a discussion on how does %.2f work? within the C Programming forums, part of the General Programming Boards category; hey, this may be a dumb question, but any help would be great. i want to find the average of ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    4

    how does %.2f work?

    hey, this may be a dumb question, but any help would be great.

    i want to find the average of some heights, to find the average height of players in a basketball team. im using scanf for retrieving the heights

    so my code gets all the heights in cm, then divids it by the number of players, but it only allows whole numbers to be inputted, and even if numbers are say for example: 3, 3, 3, 3, 3, it will return 7, instead of 7.5

    i was told using %.2f will round everthing to decimal places. is that correct?

    sorry if this is a dumb question, im very new to this.

    thanks
    Last edited by RandomHero; 08-08-2003 at 02:14 AM.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    102
    No man. No rounding off. Only truncating.
    See this link to get more idea.
    Casting
    Saravanan.T.S.
    Beginner.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    4
    thanks dude, that helped heaps, but one more question............... and i really REALLY hope you can help me with this one.

    i use gcc to compile and i accidently typed gcc -o bball_height.c bballheight

    instead of gcc -o bball_height bballheight.c

    and because i had compiled it before, bball_height did exist, so did i just lose everything?

    i sure hope not, is there any way to undo it?

    im using unix on a XP machine if that matters,

    thanks

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    4
    not to worry, i redid it, and made sure i backed it up, but the only other problem im having is limiting th decimal places. i think that is what %.2f does, but im not sure how to use it. i want it rounded to 2 decimal places.

    thanks

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    4
    You blokes (or ladies) are champions, and thanks for not yelling at me for not searching or reading the faq's, ive learnt now, and ill try to make sure it doesnt happen again.

    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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