Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16949

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Help in two decimal places Posted by Jigoku on 29 Sept 2003 at 10:57 PM
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.

Report
Re: Help in two decimal places Posted by stober on 30 Sept 2003 at 3:19 AM
: 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.
:
:
#include <stdio.h> 
#include <stdlib.h>

int main()
{
	float f = 9.0;
	printf("%.2f\n",f);
	system("pause");
	return 0;
}


Report
Re: Help in two decimal places Posted by gaetano on 8 Oct 2003 at 10:26 AM
: : 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++:
#include <iostream>
#include <iomanip>        //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;
}


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





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.