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 foriostream
Output 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::fixed
Make 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 useprintf
function.
#include <cstdio> int main() { double num = 123.45678; printf("%.2f\n", num); return 0; }
%.2f
Specifies 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 <iostream> #include <sstream> #include <iomanip> int main() { double num = 123.45678; std::stringstream ss; ss << std::fixed << std::setprecision(2) << 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 << result << std::endl; return 0; }
use
stringstream
The 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 <iostream> #include <cmath> int main() { double num = 123.45678; num = std::round(num * 100) / 100; // Round to two decimal places std::cout << num << 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 <iostream> #include <string> 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 << str << 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!