C ++ uses the IO stream to format and control the output of floating point numbers, io points

Source: Internet
Author: User

C ++ uses the IO stream to format and control the output of floating point numbers, io points
Floating Point Output (100/100 points)Description

Write a program, input a floating point number and output format requirements, according to the format requirements of the floating point number output. Given non-negative integers m and n, it indicates that the width before the decimal point of the output floating point is m. If the width is not enough, add 0 to the front and the width after the decimal point is n, if the width is not enough, add 0 to the end.(Note: WHEN n = 0, only the integer part is output. When m and n are both 0, 0 is output ).


Input description

There are two rows in each test. The first row is an integer (m), n (n> = 0, m> = 0), and the second row is a floating point number.


Output description

The floating point number output by each test sample occupies one row. The width before the decimal point is m, and the width after the decimal point is n. If the number is insufficient, the value is set to zero.

 


Sample Input
5 312.34567892 1012.34

Sample output
00012.34512.3400000000

 

 

 

AC code:

 1 #include <iostream> 2 #include <sstream> 3 #include <iomanip> 4 #include <string> 5 using namespace std; 6  7 int main(void) 8 { 9     int m, n;10     double num;11 12     while (cin >> m >> n >> num)13     {14         if (m == 0 && n == 0)15             cout << 0 << endl;16         else17         {18             cout << setw(m) << setfill('0') << int(num);19             if (n != 0)20             {21                 ostringstream os;22                 os << setiosflags(ios_base::fixed);23                 os << fixed << setprecision(n+1) << num - int(num);24                 os << setiosflags(ios_base::fixed);25                 string str = os.str();26                 for (int i = 1; i <= n+1; i++)27                     cout << str[i];28             }29             cout << endl;30         }31     }32 33     return 0;34 }

This is actually a very refined question, because the question intentionally or unintentionally dug a few traps for US (apparently intentionally, the test data table shows this point .)

 

Pay attention to the following points:

1. The width format control of integer and decimal parts must be discussed separately, rather than using setw (m + n) to limit the width of the entire number in general sense. This is because the control is not accurate enough, that is, when the integer width is sufficient and the decimal width is insufficient, the hour part may be used to find the integer part, which should not belong to its own "width ".

2. Use sstream to convert the fractional part into a string to avoid outputting 0 before the decimal point.

3. Because the fractional part is not rounded (as can be learned from the example), here we use a small trick, that is, to round one more bit (n + 1) each time ), in this case, the rounding will not affect the last decimal point to be output, but the output string will be output to the specified width n.

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.