Function Repository Resource:

RoundAwayFromZero

Source Notebook

Round a number away from zero

Contributed by: Sander Huisman

ResourceFunction["RoundAwayFromZero"][n]

rounds n away from 0.

ResourceFunction["RoundAwayFromZero"][n,a]

rounds n away from 0 to a multiple of a.

Details

ResourceFunction["RoundAwayFromZero"] has the attribute Listable.

Examples

Basic Examples (2) 

Round a number:

In[1]:=
ResourceFunction["RoundAwayFromZero"][3.14]
Out[1]=

Round a negative number:

In[2]:=
ResourceFunction["RoundAwayFromZero"][-3.14]
Out[2]=

Scope (2) 

RoundAwayFromZero is Listable:

In[3]:=
ResourceFunction["RoundAwayFromZero"][{-8.8, 4.1, 2.3, 2.9}]
Out[3]=

Round 'outwards' to a multiple of 0.25:

In[4]:=
ResourceFunction["RoundAwayFromZero"][{-8.8, 4.1, 2.3, 2.9}, 0.25]
Out[4]=

Properties and Relations (1) 

The opposite (rounding towards zero) can be achieved using IntegerPart:

In[5]:=
TableForm[
 Table[{i, ResourceFunction["RoundAwayFromZero"][i], IntegerPart[i]}, {i, {-8.8, 4.1, 2.3, 2.9}}], TableHeadings -> {None, {"Number", "Rounding away from zero", "IntegerPart"}}]
Out[5]=

Neat Examples (1) 

Plot values after rounding:

In[6]:=
Plot[ResourceFunction["RoundAwayFromZero"][x], {x, -3, 3}]
Out[6]=

Publisher

SHuisman

Version History

  • 1.0.1 – 11 October 2022
  • 1.0.0 – 12 September 2022

Related Resources

License Information