Thread: how do you format a float to 2 decimals only

  1. #1
    Unregistered
    Guest

    how do you format a float to 2 decimals only

    I am outputing float values... I guess by default they output with one decimal only. for example:
    the output is:
    12.3

    my desired output is:
    12.30

    can anyone show me how to format the float so they're at 2 decimal points?
    thank you!

    A

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I guess by outputting you mean you are using the cout object in which case you can use setprecision(2) like this...

    cout<<setprecision(2)<<myfloatvariable<<endl;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Unregistered
    Guest

    i get an error

    I get this error when I use <<setprecision(2) :

    setprecision:undeclared identifier.......what does it mean?
    thanks!

  4. #4
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    #include <iomanip.h>

    int main()
    {
    cout << fixed << showpoint << setprecision(2);

    cout << //output etc.

    return 0;

    }

  5. #5
    Unregistered
    Guest

    thank you!

    I would be sooo helpless without the help!

    Thank you!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help me
    By warthog89 in forum C Programming
    Replies: 11
    Last Post: 09-30-2006, 08:17 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Backdooring Instantaneous Radius of Curvature & Functions
    By just2peachy in forum C++ Programming
    Replies: 8
    Last Post: 10-06-2004, 12:25 PM
  4. error in program????
    By SpEkTrE in forum C Programming
    Replies: 5
    Last Post: 11-24-2003, 06:16 PM
  5. ~ Array Help ~
    By Halo in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 04:19 PM