Thread: Floating variables

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Floating variables

    When ever I print a varible which is a float it always comes up with somthing like 5.5000000 is there a way to get rid of the zeros!!!
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  2. #2
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    Yup!

    Code:
    printf("%.2f", answer);
    Limits the answer to 2 places after the decimal point.
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    thanks but....

    Yeah thanks but there is a slight problem. I don't know how many figures are going to be behind the decimal place (eg. the answer could be 5, 5.2, 5.6545, 5.364 etc.) so is there a way just to take off the useless and annoying zeros (eg. instead of having 5.3640000 have 5.364)????

    Thanks
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try %g.
    Code:
    printf("%g", answer);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  2. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  3. floating point binary program, need help
    By ph34r me in forum C Programming
    Replies: 4
    Last Post: 11-10-2004, 07:10 AM
  4. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM