Flavours of rounding
Tags: dotnet, javascript, python, java
Published: 2021-08-24 12:33:47

---
title: Flavours of rounding
published: true
date: "2021-08-24"
tags: ["dotnet", "javascript", "python", "java"]
canonical_url: https://semuserable.com/posts/flavours_of_rounding/
---

How often do you round various numbers in your day-to-day job? Do you know what type of rounding do you actually use?

Let's compare several programming languages and their **default** (meaning without additional parameters if the rounding function allows it) rounding techniques.

> I'll use several notions of rounding: [banker's rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even), [away from zero](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero) and [round half up](https://en.wikipedia.org/wiki/Rounding#Round_half_up). More info about different techniques can be found on [Wikipedia](https://en.wikipedia.org/wiki/Rounding).

## .NET

In dotnet (Framework, Core, 5+) [banker's rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) is used by default.

```csharp
Math.Round(0.5); // 0
Math.Round(1.5); // 2
Math.Round(2.5); // 2
Math.Round(3.5); // 4

Math.Round(-23.5); // -24
```

If you need [away from zero](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero), use the following

```csharp
Math.Round(0.5, MidpointRounding.AwayFromZero); // 1
Math.Round(1.5, MidpointRounding.AwayFromZero); // 2
Math.Round(2.5, MidpointRounding.AwayFromZero); // 3
Math.Round(3.5, MidpointRounding.AwayFromZero); // 4

Math.Round(-23.5, MidpointRounding.AwayFromZero); // -24
```

If you need [round half up](https://en.wikipedia.org/wiki/Rounding#Round_half_up), use the following

```csharp
Math.Round(0.5, MidpointRounding.ToPositiveInfinity); // 1
Math.Round(1.5, MidpointRounding.ToPositiveInfinity); // 2
Math.Round(2.5, MidpointRounding.ToPositiveInfinity); // 3
Math.Round(3.5, MidpointRounding.ToPositiveInfinity); // 4

Math.Round(-23.5, MidpointRounding.ToPositiveInfinity); // -23
```

## JavaScript

In JavaScript [round half up](https://en.wikipedia.org/wiki/Rounding#Round_half_up) is used be default.

```csharp
Math.round(0.5); // 1
Math.round(1.5); // 2
Math.round(2.5); // 3
Math.round(3.5); // 4

Math.round(-23.5); // -23
```

## Python (2.7, 3+)

In Python 2.7 [away from zero](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero) is used by default.

```python
round(0.5) # 1
round(1.5) # 2
round(2.5) # 3
round(3.5) # 4

round(-23.5) # -24
```

But in Python 3+ [banker's rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) is used by default.

```python
round(0.5) # 0
round(1.5) # 2
round(2.5) # 2
round(3.5) # 4

round(-23.5) # -24
```

> This was quite surprising, to be honest.

## Java

In Java (JDK 1.8.0, 9, 10, 11) [round half up](https://en.wikipedia.org/wiki/Rounding#Round_half_up) is used by default.

```java
Math.round(0.5); // 1
Math.round(1.5); // 2
Math.round(2.5); // 3
Math.round(3.5); // 4

Math.round(-23.5); // -23
```

## Go

In Go [away from zero](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero)  is used by default.

```go
math.Round(0.5) // 1
math.Round(1.5) // 2
math.Round(2.5) // 3
math.Round(3.5) // 4

math.Round(-23.5) // -24
```

But if you want [banker's rounding](https://en.wikipedia.org/wiki/Rounding#Round_half_to_even) there is a default function for this too.

```go
math.RoundToEven(0.5) // 0
math.RoundToEven(1.5) // 2
math.RoundToEven(2.5) // 2
math.RoundToEven(3.5) // 4

math.RoundToEven(-23.5) // -24
```

## PHP

In PHP [away from zero](https://en.wikipedia.org/wiki/Rounding#Round_half_away_from_zero) is used by default.

```php
round(0.5); # 1
round(1.5); # 2
round(2.5); # 3
round(3.5); # 4

round(-23.5); # -24
```