You can also use the floor() function and cell() function in place of the round() function if you want to round to the nearest integer value of the current value. This is also called truncate, and we can also use a trunc() function to round the argument towards zero and return a simple nearest integer data type value. But not larger than the magnitude of the real value. The syntax of the round function is an “integer (a floating-point value)”. In return, the floating-point value will be returned as the nearest integer value.
Round up to nearest float:
In the nearest float round-up, we use a floating-point value to round up our value and convert it into the nearest integer value.
#include <iostream>
using namespace std;
int main()
{
float a = 89.123;
cout <<"Nearest value of x :" <<round(a) <<"\n";
return 0;
}
Here we include two libraries and namespace standards and then start the main body. We initialized a float variable and displayed it with the round function.
Round up to nearest double:
Here we check our value with the data type of double and round it up with an integer data-type variable by using the round() function.
#include <iostream>
using namespace std;
int main()
{
double x = 12.578;
cout <<"Nearest value of x :" <<round(x) <<"\n";
return 0;
}
Start with libraries as well as namespace standards and write the main body of the code. In the main body, we initialized a double variable, assigned it a value, and displayed it as an integer using the round() function.
Round up to nearest multiple:
The round up to the nearest of multiple means that we round off our value, not with the truncate. We round off our values with the multiples of 10 like; if we have an integer value of 24, the code changes this value with 20, and if we have the value of 26, the code will change it with 30. Here we want to discuss the nearest multiple, but still, we get a float value to show the round() function.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int round(int n)
{
int a = (n / 10) * 10;
int b = a + 10;
return (n - a >b - n)? b : a;
}
int main()
{
int n = 472.8;
cout <<round(n) <<endl;
return 0;
}
Starting the program with the libraries <iostream>and <bit/std++>. From here, we modify our function that is round() where we pass our integer, and here we apply some mathematical operations on this integer. First, we divide and multiply this variable by 10 and store it in another variable. Then return with the comparison after subtraction. Going to the main function, we initialized a floating-point value, then applied a simple round() function and passed it to an integer variable after converting the float value into the integer. In the end, we call our customized round function.
Round up to nearest power:
The round-up function also rounds up the powers. If we enter a number, then our code tells us the number that is the power of the value that we give in the code. For example, here, we are writing the code that returns us the value that is the power of 2.
#include <cmath>
using namespace std;
unsigned findNextPower(unsigned n)
{
n = n - 1;
while (n & n - 1)
{
n = n & n - 1;
}
return n <<1;
}
int main()
{
unsigned n = 18;
cout <<"The next power of 2, from "<<n<<" is " <<findNextPower(n);
return 0;
}
Again, first of all, we use our libraries and standards after completing the protocols. We write a function that is to calculate the power of 2 and compare it with our given number. “N” is the variable that we pass to this function as an argument where we calculate the number and find the number that is the next power of 2 from that number and return it. In the main function, we just take a number and pass it to the function and return the result, and we show this result on the console.
Round up to the decimal place:
As you must know, double does not have a decimal place, so we cannot round double to two decimal places. Having binary places is not commensurable with decimal places. We have to use decimal radix when formatting for outcome with cout statement if we want the decimal places. In this example, as shown in the figure, we round off a floating-point value with two places.
using namespace std;
float round(float a)
{
float value = (int)(a * 100 + .5);
return (float)value / 100;
}
int main()
{
float a = 37.66666;
cout <<round(a);
return 0;
}
In the first line, we integrate our library, then standard and make a function. In this function, we pass a float value in its argument. Here we initialize a float value again with the parameters of integer and multiply our first variable with 100, then add “.5” and return value to the function after dividing by a hundred values. Then we start the main body of the code and initialize a variable with float data type and pass it to function and display the value.
Conclusion:
In this article, we have explained the roundup function and its header file and also described the supportive functions. We have also defined all the data types that are used to be converted into integer data-type with the help of different examples.