Home >Backend Development >C++ >How to retain the number of decimal places in C++

How to retain the number of decimal places in C++

小老鼠
小老鼠Original
2024-03-25 16:18:331749browse

In C, retaining several decimal places usually involves formatting the output. This can be achieved by using std::setprecision and std::fixed from the I/O streams library. You can use std::cout and I/O stream formatting, std::stringstream, std::round or std::floor/std::ceil for rounding, and use the C-style printf function.

How to retain the number of decimal places in C++

In C, retaining a few decimal places usually involves formatting the output, which can be done by using std from the I/O stream library: :setprecision and std::fixed to achieve this. The following are some ways to preserve the decimal places:

1. Use std::cout and I/O stream formatting

You You can use std::cout with std::fixed and std::setprecision to set the output format.

cpp

##

#include <iostream>  
#include <iomanip> // 包含 setprecision 和 fixed  
  
int main() {  
    double value = 3.141592653589793;  
    std::cout << std::fixed << std::setprecision(2) << value << std::endl; // 输出: 3.14  
    return 0;  
}

In this example, std::fixed makes sure to use fixed Point notation, and std::setprecision(2) sets the number of digits after the decimal point to 2.

2, Use std::stringstream

If you need to store the formatted string in a variable instead of outputting it directly To the console you can use std::stringstream.

cpp

##

#include <sstream>  
#include <iomanip>  
#include <string>  
  
int main() {  
    double value = 3.141592653589793;  
    std::stringstream ss;  
    ss << std::fixed << std::setprecision(2) << value;  
    std::string formatted_value = ss.str(); // formatted_value 现在包含 "3.14"  
    return 0;  
}

3. Use std::round or std::floor /std::ceil for rounding

If you want to round to a specified number of decimal places, you can use the std::round function. Please note that std::round accepts a floating point number multiplied by a power of 10 as an argument, so you need to calculate accordingly based on the number of decimal places required.

cpp

#include <cmath> // 包含 round 函数  
#include <iostream>  
  
int main() {  
    double value = 3.141592653589793;  
    double rounded_value = std::round(value * 100.0) / 100.0; // 四舍五入到小数点后两位  
    std::cout << rounded_value << std::endl; // 输出: 3.14  
    return 0;  
}

If you want to round down or round up To specify the number of decimal places, you can use the std::floor or std::ceil functions, combined with appropriate multiplication operations.

4. Use C-style formatted output

Although C recommends using I/O streams for formatting, you can also use C style printf function.

cpp

##

#include <cstdio>  
  
int main() {  
    double value = 3.141592653589793;  
    printf("%.2f\n", value); // 输出: 3.14  
    return 0;  
}
In printf, %.2f means outputting a floating point number , and keep to two decimal places.

These methods can be used to retain several decimal places in C. Which method you choose depends on your specific needs and which programming style you prefer.

The above is the detailed content of How to retain the number of decimal places in C++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn