Skip to content

Commit

Permalink
[New Term] C++ Math Functions: round() (#1043)
Browse files Browse the repository at this point in the history
* New Entry: C++ Math Functions: `round()` (#986)

* fix: removed unwanted fun `round()` from `main` (#986)

* New Entry: C++ Math Functions: `round()` (#986)

* fix: sentences rephrased (#986)

* ⚡ fix: update example and sentences rephrased (#986)

* ⚡ refactor: cpp math-function `round()` (#986)

* ⚡ refactor: cpp math-function `round()` (#986)

* ⚡ refactor: cpp math-function `round()` (#986)

* fix: changes and reword sentences

* 🚀 fix: compile time error

* 🚀 fix: single line to `list` comment

* Update round.md
  • Loading branch information
tanishq-singh-2407 authored Oct 24, 2022
1 parent f2d7350 commit cc27e6e
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions content/cpp/concepts/math-functions/terms/round/round.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
---
Title: 'round()'
Description: 'Returns the integer that is nearest to the argument, with halfway cases rounded away from the ending zero.'
Subjects:
- 'Computer Science'
Tags:
- 'Functions'
- 'Arithmetic'
- 'Methods'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

The **`round()`** function returns the integer that is closest to the argument, with halfway cases rounded away from the ending zero.

## Syntax

```pseudo
round(num);
```

- The `num` parameter must be a `double`,`float`, or `long double`.
- The return value will be an integer.
- If the decimal in `num` is `0.5` or higher, the closest integer greater than `num` is returned.

## Example

The following example showcases the `round()` function being applied to two `double` values, one of which is a halfway case:

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

int main() {
double num1 = 9.23;
double result1;
result1 = std::round(num1);

std::cout << "The result of round(9.23) is " << result1 << "\n";

double num2 = 4.5;
double result2;
result2 = std::round(num2);

std::cout << "The result of round(4.5) is " << result2 << "\n";
}
```

This produces the following output:

```shell
The result of round(9.23) is 9
The result of round(4.5) is 5
```

## Codebyte Example

The following example is runnable and rounds the halfway case away from zero:

```codebyte/cpp
#include <iostream>
#include <cmath>
int main() {
double num = 10.89;
double result;
result = std::round(num);
std::cout << "The result is " << result << "\n";
}
```

0 comments on commit cc27e6e

Please sign in to comment.