Rounding Values in Different Programming Languages

After discussing the subject of my previous CodeProject publication with others, I got curious how other languages handle the mid point values. I was happily surprised that almost every language did have this subject covered. However, there are a lot of differences between languages.

To hopefully help someone in the future, I have mapped the rounding methods in a table. X’s in bold are default implementations, other X’s are optional parameters or separate methods.

The language names in the table link to the documentation I used.

Without further ado, the big “programming language/rounding method table”:

  Java C Python* C++ VB .NET JavaScript C# PHP Objective C MATLAB R Perl Swift Go Delphi Ruby**
Round half up X X       X   X                
Round half down   X           X                
Round half towards zero   X                           X
Round half away from zero   X   X X   X   X X   X X X   X
Round half to even   X X   X   X X     X X   X X X
Round half to odd               X       X        
Random tie-breaking                       X        

* The table references Python 3.7 but Python 2 actually has a different round implementation. It will round half away form zero.
** The mode keywords in Ruby are :up for “round half away from zero” and :down for “round half towards zero”, making them quite confusing in my opinion.

Of course, I’ve added this table to the article as well. Check it out here:

https://www.codeproject.com/Tips/1272542/How-NETs-Math-Round-has-Nothing-to-do-with-Maths-A