Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help in two decimal places

JigokuJigoku Member Posts: 1
hi i'm new in c++
and i need some help to output 2 decimal places in float.
example:
if the output is 9.9(float) i want to be 9.90(float)
or 35(float) i want to be 35.00(float)
and so on.

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    : hi i'm new in c++
    : and i need some help to output 2 decimal places in float.
    : example:
    : if the output is 9.9(float) i want to be 9.90(float)
    : or 35(float) i want to be 35.00(float)
    : and so on.
    :
    :
    [code]
    #include
    #include

    int main()
    {
    float f = 9.0;
    printf("%.2f
    ",f);
    system("pause");
    return 0;
    }
    [/code]

  • gaetanogaetano Member Posts: 48
    : : hi i'm new in c++
    : : and i need some help to output 2 decimal places in float.
    : : example:
    : : if the output is 9.9(float) i want to be 9.90(float)
    : : or 35(float) i want to be 35.00(float)
    : : and so on.

    And now the way you're solving problems like that in C++:
    [code]
    #include
    #include //that's the header file you need in this
    case!
    using namespace std;

    int
    main (void)
    {
    float f = 9.9;

    cout << setprecision (2) << f << endl;

    return 0;
    }
    [/code]

    Now let's just say something about the source code you might understand but which functions you don't know!

    `setprecision ()' is a so called IO-Manipulator (therefor he stands in the iomanip!)

    You might want to take a look at a reference of the c++ standard library! For example: http://www.cplusplus.com/ref/iostream/iomanip/index.html


Sign In or Register to comment.