Help in two decimal places - Programmers Heaven

#### Howdy, Stranger!

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

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Help in two decimal places

Posts: 1Member
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.

• Posts: 9,765Member ✭✭✭
: 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]

• Posts: 48Member
: : 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