C round() Function

The round() function in C is a standard library function that rounds a given floating-point number to the nearest integer. It is part of the C standard library (math.h). This function is useful for performing mathematical rounding operations where the value is rounded to the nearest whole number.

Table of Contents

  1. Introduction
  2. round() Function Syntax
  3. Understanding round() Function
  4. Examples
    • Rounding a Value
    • Using round() with User Input
  5. Real-World Use Case
  6. Conclusion

Introduction

The round() function calculates the nearest integer to a given floating-point number ( x ). If the fractional part of ( x ) is 0.5 or greater, the function rounds away from zero, otherwise, it rounds towards zero.

round() Function Syntax

The syntax for the round() function is as follows:

#include <math.h>
double round(double x);

Parameters:

  • x: The floating-point value to be rounded.

Returns:

  • The function returns the nearest integer to x as a floating-point number.

Understanding round() Function

The round() function takes a floating-point number ( x ) and returns the nearest integer value. If the fractional part of ( x ) is 0.5 or greater, the result is rounded away from zero; otherwise, it is rounded towards zero.

Examples

Rounding a Value

To demonstrate how to use round() to round a value to the nearest integer, we will write a simple program.

Example

#include <stdio.h>
#include <math.h>

int main() {
    double value = 3.14;

    // Compute the rounded value
    double result = round(value);

    // Print the result
    printf("Rounded value of %.2f is: %.0f\n", value, result);

    return 0;
}

Output:

Rounded value of 3.14 is: 3

Using round() with User Input

This example shows how to use round() to round a value provided by the user.

Example

#include <stdio.h>
#include <math.h>

int main() {
    double value;

    // Get user input for the value
    printf("Enter a value: ");
    scanf("%lf", &value);

    // Compute the rounded value
    double result = round(value);

    // Print the result
    printf("Rounded value of %.2f is: %.0f\n", value, result);

    return 0;
}

Output (example user input "2.7"):

Enter a value: 2.7
Rounded value of 2.70 is: 3

Real-World Use Case

Calculating Final Prices in Financial Applications

In real-world applications, the round() function can be used to calculate final prices in financial applications, ensuring prices are rounded to the nearest whole number.

Example: Calculating Final Prices

#include <stdio.h>
#include <math.h>

int main() {
    double price;

    // Get user input for the price
    printf("Enter the price: ");
    scanf("%lf", &price);

    // Calculate the final rounded price
    double final_price = round(price);

    // Print the result
    printf("The final rounded price is: %.0f\n", final_price);

    return 0;
}

Output (example user input price "19.95"):

Enter the price: 19.95
The final rounded price is: 20

Conclusion

The round() function is essential for rounding a floating-point number to the nearest integer in C. It is useful in various mathematical calculations, particularly in fields like finance, engineering, and general software development, where rounding to the nearest whole number is required.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare