Decimal point Output precision control problem

Source: Internet
Author: User
Tags setf

decimal point output precision control problem

SETF () is a function that appends the label word, and flags () is the label word

Fixed flag is to display floating-point numbers in fixed-point form

When there is a fixed mark, indicating that the data according to a certain number of digits output, or remove the fixed mark, the data in situ output---that is the number of the last 0 does not show

Therefore, there are two things to use:

(1) in-situ output, the fixed sign should be removed: cout. UNSETF (ios::fixed)

If 0.1 will output 0.1

(2) Set the number of output digits after the decimal point

A two-step:

The first step---------set the positioning output cout. SETF (ios::fixed)

The second step---------set the output precision cout. Precision (4)

As the following program:

#include <iostream.h>
#include <iomanip.h>
void Main (void)
{
COUT.SETF (ios::fixed);
Cout<<setprecision (2) << (float) 0.1<<endl;//output 0.10
COUT.UNSETF (ios::fixed);
Cout<<setprecision (2) << (float) 0.1<<endl; Output 0.1
}

fout<<rs<< '/t ' << '/t ' <<dr_mean<< ' (";

FOUT.SETF (ios::fixed);

fout<<dr_sd<< ")" <<endl;

FOUT.UNSETF (ios::fixed);

fout<< '/t ' << '/t ' <<mature_mean<< ' (";

FOUT.SETF (ios::fixed);
Fout.precision (4);

fout<<mature_sd<< ")" <<endl;

FOUT.UNSETF (ios::fixed);

fout<< '/t ' << '/t ' <<cost_mean<< ' (";


FOUT.SETF (ios::fixed);
Fout.precision (6);


fout<<cost_sd<< ")" <<endl;

FOUT.UNSETF (ios::fixed);

The output results are as follows:

0.998836---in situ (0.000000)---six-bit

2746.2---in situ

(99.7445)--four-bit

3.152--in situ

(0.015522)--six-bit

Other references are as follows:

* * about the format of floating-point numbers * *
#include <iostream.h>
void Main ()
{
float f=2.0/3.0,f1=0.000000001,f2=-9.9;
cout<<f<< ' <<f1<< ' <<f2<<endl; Normal output
COUT.SETF (Ios::showpos); Force Plus + before positive number
cout<<f<< ' <<f1<< ' <<f2<<endl;
COUT.UNSETF (Ios::showpos); Remove positive number before Plus +
COUT.SETF (Ios::showpoint); Force display of invalid 0 after decimal point
cout<<f<< ' <<f1<< ' <<f2<<endl;
COUT.UNSETF (Ios::showpoint); Suppresses invalid 0 after the decimal point is displayed
COUT.SETF (ios::scientific); Scientific method of Accounting
cout<<f<< ' <<f1<< ' <<f2<<endl;
COUT.UNSETF (ios::scientific); Elimination of scientific notation
COUT.SETF (ios::fixed); Display by point output
cout<<f<< ' <<f1<< ' <<f2<<endl;
COUT.UNSETF (ios::fixed); Cancel the dot output display
Cout.precision (18); Accuracy is 18, normal is 6
cout<<f<< ' <<f1<< ' <<f2<<endl;
Cout.precision (6); Precision restored to 6
}
Manipulation operator:
* * about the format of floating-point numbers * *
#include <iomanip.h>
void Main ()
{
float f=2.0/3.0,f1=0.000000001,f2=-9.9;
cout<<f<< ' <<f1<< ' <<f2<<endl; Normal output
Cout<<setiosflags (Ios::showpos); Force Plus + before positive number
cout<<f<< ' <<f1<< ' <<f2<<endl;
Cout<<resetiosflags (Ios::showpos); Remove positive number before Plus +
Cout<<setiosflags (Ios::showpoint); Force display of invalid 0 after decimal point
cout<<f<< ' <<f1<< ' <<f2<<endl;
Cout<<resetiosflags (Ios::showpoint); Suppresses invalid 0 after the decimal point is displayed
Cout<<setiosflags (ios::scientific); Scientific method of Accounting
cout<<f<< ' <<f1<< ' <<f2<<endl;
Cout<<resetiosflags (ios::scientific); Elimination of scientific notation
Cout<<setiosflags (ios::fixed); Display by point output
cout<<f<< ' <<f1<< ' <<f2<<endl;
Cout<<resetiosflags (ios::fixed); Cancel the dot output display
Cout<<setprecision (18); Accuracy is 18, normal is 6
cout<<f<< ' <<f1<< ' <<f2<<endl;
Cout<<setprecision (6); Precision restored to 6
}

Resources:

Http://zhidao.baidu.com/question/9029358.html

Http://zhidao.baidu.com/question/77286598.html

Http://bbs.bccn.net/thread-150172-1-1.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.