How to Round to Two Decimal Places in C++?

Round to Two Decimal Places in CPP

Do you want to learn more about rounding floating point numbers in CPP (C++) and how to round off to two decimal places in CPP? 🤔

The float and double datatypes in C++ can be used to represent decimal numbers. Every type comes in a particular size and range. The float type uses four bytes of memory and has a maximum precision of six digits. The double type requires eight bytes of memory and has a fifteen-digit precision limit.

In this article, we’ll discuss the methods for rounding integers to one decimal place in CPP. Both float and double-type variables can be used with these techniques.

 

 

What is the Round() Function in CPP (C++)?

Using the round function, a given digit in a float or double is rounded off. With midway situations rounded away from zero, it delivers the integral value that is closest to the parameter specified in a round function. 

The round function in C++ is a mathematical function that returns the integral value. In other words, this function is used to round off any given data type, such as double and float (excluding string type), to the nearest integral value. 

You may alternatively use std::round() instead of round()Round() offers practical guidance to speed up the process as well as assistance in resolving applications that struggle with the relationship between fractions and decimals.

 

Syntax of round() function in CPP.

Return_datatype round(data_type variable);

 

Code

#include <iostream>

using namespace std;




int main() {

  cout << round(19.5);

  return 0;

}

 

Output

 20

 

 

How to Round to Two Decimal Places in CPP (C++)?

There are different approaches that can be used to round a value to one decimal place.  formatting strings, accuracy parameters, and the math library’s round() function. Let’s examine each one in turn. using correct syntax and code illustrations.

Following are the three approaches given below;

  1. Use float precision.
  2. Use round() function.
  3. Use speintf() and sscanf().

 

Method 1: Use Float Precision

The printf() function in CPP is used to indicate printing with formatting. The formatting string must be set in advance in order to show some data using printf() operations. C++ has a similar printf() function as well. The formatting syntax for integers expressed in certain decimal places will look like the following.

 

Code

#include <iostream>


using namespace std;



int main()

{

               float variable = 47.5557;

               cout << "  The Number 47.5557 up to 2 decimal places: ";

               printf("%.2f", variable);

               return 0;

}

 

Output

Number 47.5557 up to 2 decimal places: 47.56

 

We can observe that the supplied integer, in this case, has up to four decimal points. However, we are showing it with three decimal places. Additionally, when rounding off, it automatically converts to the nearest amount.

 

 

Method 2: Use round()  Function

To further illustrate possible solutions, we can also use the round function to round a value to two decimal places. Let’s see an example.

 

Code

#include <iostream>

using namespace std;




float round(float variable)

{

               float value = (int)(variable * 100 + 0.5);

               return (float)value / 100;

}




int main()

{

               float variable = 47.5557;

               cout << "  The Number 47.5557 up to 2 decimal places: ";

               cout << round(variable);

               return 0;

}

 

Output

The Number 47.5557 up to 2 decimal places: 47.56

 

It is clear from this example that using the round() method is the appropriate and convenient technique to round a floating point value to the two decimal place. This method returns the integer equivalent of the number as an argument.

 

 

Method 3: Use The speintf() And sscanf() Functions

By supplying the appropriate format specifier, the sscanf() function scans values from a char[] array and stores each value into a variable with the appropriate data type. The sprintf() function reads one or more values supplied with their corresponding format specifiers, stores them in a char[] array, and then returns them.

 

Code

#include <iostream>

using namespace std;




float round(float variable)

{

               char string[50];

               sprintf(string, "%.2f", variable);

               sscanf(string, "%f", &variable);

               return variable;

}



int main()

{

               float variable = 47.5557;

               cout << "  The Number 47.5557 up to 2 decimal places: ";

               cout << round(variable);

               return 0;

}


Output

The Number 47.5557 up to 2 decimal places: 47.56

 

In the above example, we save the number as a string using an array of characters.  Print the variable’s value with two decimal places as a string, and then scan the value of the string in the variable.

 

 

Conclusion

To summarize the article, how to round to two decimal places In CPP (C++), we have discussed three different approaches through which we can round a value to two decimal places. A decimal can be typecast to an integer, and then converted using some calculation. 

The float precision() method allows us to modify the output stream’s precision digits. The %.2f format specifier can be used with the printf() and fprintf() functions to round values to two decimal places. We can also use the ceil() and floor() functions to round up to two decimal places.

The most efficient way to round off a value up to two decimal places is to use the speintf() And sscanf() functions. As speintf()  function is used to scan value from a char array and store it into a datatype, whereas sscanf() function is used to read and specify format specifier and return value in a char array.

Let’s have a quick recap of the topics discussed in this article.

  1. What is round in the CPP?
  2. Use float precision.
  3. Use the round() function.
  4. Use the speintf() and sscanf() functions.

If you’ve found this article helpful, don’t forget to share and comment below. 👇: Which solutions have helped you solve the problem?

 

 

 

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share