2024-11-20 12:56:32 +01:00

75 lines
2.3 KiB
Markdown

---
layout: default
---
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
<h1 id="function-round">Function round <a href="#function-round" title="Permalink">#</a></h1>
Round a value towards the nearest rounded value.
For matrices, the function is evaluated element wise.
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.round(x)
math.round(x, n)
math.round(unit, valuelessUnit)
math.round(unit, n, valuelessUnit)
```
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`x` | number &#124; BigNumber &#124; Fraction &#124; Complex &#124; Unit &#124; Array &#124; Matrix | Value to be rounded
`n` | number &#124; BigNumber &#124; Array | Number of decimals Default value: 0.
`valuelessUnit` | Unit | A valueless unit
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
Type | Description
---- | -----------
number &#124; BigNumber &#124; Fraction &#124; Complex &#124; Unit &#124; Array &#124; Matrix | Rounded value
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
Type | Description
---- | -----------
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
```js
math.round(3.22) // returns number 3
math.round(3.82) // returns number 4
math.round(-4.2) // returns number -4
math.round(-4.7) // returns number -5
math.round(3.22, 1) // returns number 3.2
math.round(3.88, 1) // returns number 3.9
math.round(-4.21, 1) // returns number -4.2
math.round(-4.71, 1) // returns number -4.7
math.round(math.pi, 3) // returns number 3.142
math.round(123.45678, 2) // returns number 123.46
const c = math.complex(3.2, -2.7)
math.round(c) // returns Complex 3 - 3i
const unit = math.unit('3.241 cm')
const cm = math.unit('cm')
const mm = math.unit('mm')
math.round(unit, 1, cm) // returns Unit 3.2 cm
math.round(unit, 1, mm) // returns Unit 32.4 mm
math.round([3.2, 3.8, -4.7]) // returns Array [3, 4, -5]
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[ceil](ceil.html),
[fix](fix.html),
[floor](floor.html)