Home  >  Article  >  Backend Development  >  C++ program to round a number to n decimal places

C++ program to round a number to n decimal places

PHPz
PHPzforward
2023-09-12 17:13:021616browse

C++ program to round a number to n decimal places

Representing numbers as output is an interesting and important task when writing a program in any language. For integer types (data of type short, long, or medium), it is easy to represent numbers as output. For floating point numbers (float or double type), sometimes we need to round them to a specific number of decimal places. For example, if we want to represent 52.24568 as three decimal places, some preprocessing is required. In this article, we will introduce several techniques to represent floating point numbers to a specific number of decimal places by rounding.

Among the different methods, it is important to use a C-like format string, use the precision parameter, and use the round() function in the math library. Let’s look at them one by one. With correct syntax and code examples.

Use format string

In C language, we use the printf() function to represent formatted printing. To use the printf() function to display some data, you need to specify the format string in advance. The same printf() function also works in C. To represent a number with a specific number of decimal places, the formatting syntax would be as follows

grammar

The syntax of the printf statement.

printf ( “%.<number of decimal place>f”, <floating point number> );

For example, if we want to display a floating point variable NUM, retaining 4 decimal places, the statement will be like this -

printf ( “%.4f”, NUM );
The Chinese translation of

Example

is:

Example

#include <iostream>
using namespace std;
void solve( float number) {
   printf ( "%.3f", number );
}
int main(){
   cout << "Number 45.278586 up to 3 decimal places: ";
   solve( 45.278586 );
}

Output

Number 45.278586 up to 3 decimal places: 45.279

In this example, we can see that the given number has 6 decimal places. But we only display to 3 decimal places. And automatically converted to the nearest value when rounded. However, there is a drawback to this process. We cannot dynamically change the decimal place value at any time. To overcome this problem, we can take another approach using the C-based setprecision() method.

Use setprecision method

C There is a special formatting function called setprecision(), which is used to set the precision value to up to n decimal places. To use this method, we need to import the iomanip library. It is also necessary to specify that we use a fixed number of decimal places. The syntax is as follows:

grammar

Define set precision() method

include <iomanip>
std::cout << std::fixed;
std::cout << std::setprecision( <number of decimal places> );
std::cout << The_floating_point_number;

For example, if we want to display a floating point variable NUM, retaining 4 decimal places, the statement will be like this -

include <iomanip>
std::cout << std::fixed;
std::cout << std::setprecision( 4 );
std::cout << NUM;
The Chinese translation of

Example

is:

Example

#include <iostream>
#include <iomanip>

using namespace std;
void solve( float number, int place) {
   cout << fixed;
   cout << setprecision( place );
   cout << number << endl;
}
int main(){
   cout << "Number 45.278586 up to 3 decimal places: ";
   solve( 45.278586, 3);
   cout << "Number 45.278586 up to 4 decimal places: ";
   solve( 45.278586, 4);
   cout << "Number 45.278586 up to 5 decimal places: ";
   solve( 45.278586, 5);
}

Output

Number 45.278586 up to 3 decimal places: 45.279
Number 45.278586 up to 4 decimal places: 45.2786
Number 45.278586 up to 5 decimal places: 45.27859

This is an ideal way to represent n digits after the decimal point. Sometimes when n = 0, we can use another method to round. This will convert the number to an integer. The specific method is as follows −

Use round() method

The "cmath" library has a round() method to convert a number to its nearest integer. So this is converting a floating point number to 0 decimal places. The syntax is as follows.

grammar

Use round() method

include <cmath>
float res = round ( <floating point number> );

For example, if we wanted to round the number 45.254 to the nearest integer, the statement would look like this.

include <cmath>
float res = round ( 45.254 );
std::cout << res;
The Chinese translation of

Example

is:

Example

#include <iostream>
#include <cmath>

using namespace std;
void solve( float number) {
   float res;
   res = round ( number );
   cout << res << endl;
}
int main(){
   cout << "Number 45.278586 to its nearest integer: ";
   solve( 45.278586 );
   cout << "Number 89.7854 to its nearest integer: ";
   solve( 89.7854 );
   cout << "Number -45.69 to its nearest integer: ";
   solve( -45.69 );
}

Output

Number 45.278586 to its nearest integer: 45
Number 89.7854 to its nearest integer: 90
Number -45.69 to its nearest integer: -46

In this example, it is obvious that the appropriate and simple way to convert a floating point number to the nearest integer is to use the round() function. This function takes a number as argument and returns the integer equivalent. In our example, we have a negative number -45.69, and after rounding it, it becomes -46, which is smaller than the original number. So the round() method is not like floor() or ceil().

in conclusion

When we write code in C, there are few ways to represent floating point numbers with up to n decimal places. The most basic way is to use the printf() method and a format string. However, with this method, the format string decimal places cannot be changed dynamically. To handle this problem, the C iomanip library has the set precision() method, which gets the number of decimal places to which a floating point number should be rounded. Sometimes we need to round a floating point number to the nearest integer (0 decimal places), in this case we can use the round() method from the cmath library in C.

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

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete