---
title: round()
url: https://docs.vultr.com/cpp/standard-library/cmath/round
description: Round to Nearest Integer
publish_date: 2024-11-12T18:20:00.067121Z
last_updated: 2024-11-12T18:20:00.067121Z
header_image: https://docs.vultr.com/public/doc-assets/collection-items/777/9f71d32d-9b2e-4694-8516-427c6bafea34.jpg
---

> Index of every Vultr Docs page in markdown: https://docs.vultr.com/llms.txt

# C++ cmath round() - Round to Nearest Integer

## Introduction
The `round()` function in C++ belongs to the `<cmath>` library and provides the necessary utility to round floating-point numbers to the closest integer. This function is integral in scenarios where precise decimal values need approximation to the nearest whole number, which is common in financial calculations, statistical data processing, and graphical computations.

In this article, you will learn how to employ the `round()` function in your C++ programs to effectively handle floating-point numbers. We will explore basic rounding operations and take a look at how `round()` behaves with different numerical values.

## Using round() in Basic Scenarios

### Round a Single Floating-Point Number
1. Include the `<cmath>` library to access the `round()` function.
1. Define a floating-point variable.
1. Apply `round()` and print the result.

    ```cpp
    #include <cmath>
    #include <iostream>
    
    int main() {
        double num = 3.7;
        double result = round(num);
        std::cout << "Rounded value: " << result << std::endl;
    }
    ```

    This code snippet rounds the value `3.7` to `4.0`. The `round()` function computes the nearest integer and, despite returning a floating-point number, the result represents an integer value.

### Round Negative Numbers
1. Understand that `round()` also handles negative numbers by moving to the nearest whole number.
1. Assign a negative floating-point value.
1. Round the number and display the result.

    ```cpp
    #include <cmath>
    #include <iostream>
    
    int main() {
        double num = -2.3;
        double result = round(num);
        std::cout << "Rounded value: " << result << std::endl;
    }
    ```

    For negative numbers, `round()` moves to the nearest whole number, rounding `-2.3` up to `-2.0`. The behavior remains consistent with positive numbers, focusing solely on the proximity to the nearest integer.

## Handling Edge Cases

### Round Numbers at .5 Boundary
1. Realize that traditional rounding methods may vary, but `round()` has standard behavior in C++.
1. Test `round()` with a number exactly halfway between two integers.

    ```cpp
    #include <cmath>
    #include <iostream>

    int main() {
        double num = 2.5;
        double result = round(num);
        std::cout << "Rounded value: " << result << std::endl;
    }
    ```

    `round()` in this scenario rounds `2.5` up to `3.0`. The function adheres to the rule of rounding to the nearest even number in cases of a tie. 

## Conclusion
The `round()` function from the `<cmath>` library in C++ provides a robust method for handling rounding of floating-point numbers to the nearest integer. This function can significantly simplify numerical data processing by automatically handling the rounding of values, reducing both the amount of code and the potential for rounding errors. By mastering `round()`, enhance the accuracy and efficacy of your numerical computations in various C++ projects. The examples provided guide you on how to apply this function in practical coding situations.