llround() Function
The llround() function rounds a floating-point number to the nearest integer value, with halfway cases rounded away from zero. It returns the result as a long long integer, making it useful for converting floating-point values to integer values while ensuring proper rounding behavior.
Syntax of llround()
long long int llround(double x);
long long int llroundf(float x);
long long int llroundl(long double x);Parameters
| Parameter | Description | 
|---|---|
| x | The floating-point value to round. | 
Return Value
The function returns the value rounded to the nearest integral value, cast to a type long long int. If the rounded value falls outside the range representable by the return type, an error may occur, with behavior dependent on the implementation.
Additional details: The function rounds halfway cases away from zero. If a domain error occurs (for example, when the value cannot be rounded properly), and if math_errhandling is set accordingly, errno will be set to EDOM or the appropriate floating-point exception will be raised. Similarly, for an overflow range error, errno may be set to ERANGE or the FE_OVERFLOW exception raised.
Examples for llround()
Example 1: Rounding a Positive Floating-Point Value
This example demonstrates how a positive floating-point value is rounded to its nearest integer value.
Program
#include <stdio.h>
#include <math.h>
int main() {
    double value = 3.6;
    long long int result = llround(value);
    
    printf("Rounded value: %lld\n", result);
    return 0;
}Explanation:
- A double variable is initialized with the value 3.6.
- The llround()function rounds the value to the nearest integer, resulting in4.
- The rounded value is stored and then printed.
Program Output:
Rounded value: 4Example 2: Rounding a Negative Floating-Point Value with a Halfway Case
This example shows how the function handles a negative value, specifically when the number is exactly halfway between two integers, ensuring that it rounds away from zero.
Program
#include <stdio.h>
#include <math.h>
int main() {
    double value = -2.5;
    long long int result = llround(value);
    
    printf("Rounded value: %lld\n", result);
    return 0;
}Explanation:
- A double variable is set to -2.5.
- The llround()function rounds-2.5away from zero, resulting in-3.
- The result is stored and printed.
Program Output:
Rounded value: -3Example 3: Rounding a Value Near a Halfway Boundary
This example illustrates the behavior of llround() when the floating-point value is just below the halfway point between two integers.
Program
#include <stdio.h>
#include <math.h>
int main() {
    double value = 2.4999;
    long long int result = llround(value);
    
    printf("Rounded value: %lld\n", result);
    return 0;
}Explanation:
- A double variable is initialized with 2.4999.
- The llround()function rounds the value to the nearest integer, resulting in2since it is just below the halfway mark.
- The rounded value is then stored and printed.
Program Output:
Rounded value: 2