SoFunction
Updated on 2025-04-11

Common ways to preserve data from two decimal places

In C++, preserving the two decimal places after the data usually involves processing floating point numbers. Here are some common ways to achieve this requirement:

Use the setupprecision in the <iomanip> library

This is one of the most commonly used methods, suitable foriostreamOutput streams in the library, such asstd::cout

#include <iostream>
#include <iomanip>
 
int main() {
    double num = 123.45678;
    std::cout << std::fixed << std::setprecision(2) << num << std::endl;
    return 0;
}
  • std::fixedMake sure numbers are output in fixed decimal format.

  • std::setprecision(2)Set the decimal point and keep two digits.

Use printf function

If you prefer C-style output, you can useprintffunction.

#include <cstdio>
 
int main() {
    double num = 123.45678;
    printf("%.2f\n", num);
    return 0;
}
  • %.2fSpecifies the output floating point number, retaining two decimal places.

Using stringstream and setupcision

If you need to store the formatted numbers as strings, you can usestringstream

#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;iomanip&gt;
 
int main() {
    double num = 123.45678;
    std::stringstream ss;
    ss &lt;&lt; std::fixed &lt;&lt; std::setprecision(2) &lt;&lt; num;/*stringstream
 std::stringstream ss;: Create a stringstream object ss to handle string stream*/
    std::string result = ();/* Use the() method to convert the contents in the stringstream object ss into a std::string type string and assign it to the variable result_str.  At this time, the value of result_str is "3.33"*/
    std::cout &lt;&lt; result &lt;&lt; std::endl;
    return 0;
}
  • usestringstreamThe formatted numbers can be converted into strings with flexibility.

Rounding using mathematical methods

If you need to process numbers directly in your calculations, rather than just for output, you can use mathematical methods to round to two decimal places.

#include &lt;iostream&gt;
#include &lt;cmath&gt;
 
int main() {
    double num = 123.45678;
    num = std::round(num * 100) / 100;  // Round to two decimal places    std::cout &lt;&lt; num &lt;&lt; std::endl;
    return 0;
}

std::round(num * 100) / 100 Multiply the number by 100, round to the nearest integer, and then divide by 100, thus achieving the effect of retaining two decimal places.

Use to_string and string processing (not recommended)

While it is possible to convert a floating point number to a string using std::to_string and then manually intercept the two decimal places, this approach is not recommended because it does not round, it simply truncates.

#include &lt;iostream&gt;
#include &lt;string&gt;
 
int main() {
    double num = 123.45678;
    std::string str = std::to_string(num);
    size_t dot_pos = ('.');
    if (dot_pos != std::string::npos) {
        str = (0, dot_pos + 3);  // Intercept the two decimal places    }
    std::cout &lt;&lt; str &lt;&lt; std::endl;
    return 0;
}

This method simply intercepts the string without rounding it, so it should be avoided when precise control of the number of decimal places is required.

Summarize

  • For output, it is recommended to use std::fixed and std::setprecision or printf.
  • For numbers that need to be processed in the calculation, use std::round for rounding.
  • Avoid using simple string intercept method, as it does not round.

This is the article about the common methods of C++ to retain the two decimal places after data. For more related content on C++ data, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!