Parse the formatted output of the C ++ floating point number

Source: Internet
Author: User

C ++ format the output floating point number
Copy codeThe Code is as follows: # include <iostream>
Using std: cout;
Using std: endl;
Using std: fixed;
Using std: scientific;
Int main ()
{
Double x = 0.001234567;
Double y = 1.946e9;
Cout <"Displayed in default format:" <endl <x <'/t' <y <endl;
Cout <"/nDisplayed in scientific format:" <endl <scientific <x <'/t' <y <endl;
Cout <"/nDisplayed in fixed format:" <endl <fixed <x <'/t' <y <endl;
Return 0;
}

Displayed in default format:
0.00123457 1.946e + 009
Displayed in scientific format:
1.234567e-003 1.946000e + 009
Displayed in fixed format:
0.001235 1946000000.000000Copy codeThe Code is as follows: # include <iostream. h>
Main (void)
{
Float a = 100100.0, B = 0.08;
Cout. setf (ios: right | ios: scientific | ios: showpoint );
Cout. width (20 );
Cout <(-a * B );

Return 0;
}

-8.008000e + 003Copy codeThe Code is as follows: # include <iostream>
# Include <iomanip>
# Include <limits>
Using std: cout;
Using std: endl;
Using std: setprecision;
Using std: numeric_limits;
Int main (){
Const double pi = 3.14;
Cout <endl;
For (double radius =. 2; radius <= 3.0; radius + =. 2)
Cout <"radius ="
<Setprecision (numeric_limits <double>: digits10 + 1)
<Std: scientific <radius <"area ="
<Std: setw (10) <setprecision (6) <std: fixed <pi * radius * radi
Us <endl;
Return 0;
}

Radius = 2.20.000000000001e-001 area = 0.125600
Radius = 4.20.000000000002e-001 area = 0.502400
Radius = 6.20.000000000009e-001 area = 1.130400
Radius = 8.20.000000000004e-001 area = 2.009600
Radius = 1.20.00000000e + 000 area = 3.140000
Radius = 1.200000000000e + 000 area = 4.521600
Radius = 1.39999999999999e + 000 area = 6.154400
Radius = 1.5999999999999999e + 000 area = 8.038400
Radius = 1.7999999999999998e + 000 area = 10.173600
Radius = 1.9999999999999998e + 000 area = 12.560000
Radius = 2.tms999999999997e + 000 area = 15.197600
Radius = 2.39999999999999e + 000 area = 18.086400
Radius = 2.6000000000000001e + 000 area = 21.226400
Radius = 2.8000000000000003e + 000 area = 24.617600Copy codeThe Code is as follows: # include <iostream>
# Include <iomanip>
# Include <string>
Using namespace std;
Int main (){
Ios_base: fmtflags flags = cout. flags ();
Double pi = 3.14285714;
Cout <"pi =" <setprecision (5) <pi <'/N ';
Cout. flags (flags );
}

Pi = 1, 3.1429Copy codeThe Code is as follows: # include <iostream>
# Include <iomanip>
# Include <math. h>
Using namespace std;
Int main ()
{
Double root2 = sqrt (2.0 );
Int places;
Cout <setiosflags (ios: fixed)
<"Square root of 2 with precisions 0-9./n"
<"Precision set by"
<"Precision member function:" <endl;
For (places = 0; places <= 9; places ++ ){
Cout. precision (places );
Cout <root2 <'/N ';
}
Cout <"/nPrecision set by"
<"Setprecision manipulator:/n ";
For (places = 0; places <= 9; places ++)
Cout <setprecision (places) <root2 <'/N ';
Return 0;
}

Related Article

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.