Math.round vs Math.floor vs Math.ceil : Kotlin

Rounding up to the nearest Integer value functionality is something required a lot of times. Kotlin has a few inbuilt functions which can do the rounding up for us but they are a bit confusing. To decide when to use what, we need to understand which rounding function rounds up in which direction and which data types it can round up.

This post was originally posted at https://agrawalsuneet.github.io/blogs/math-round-vs-math-floor-vs-math-ceil-kotlin/ and reposted on Medium on 13th April 2022.

Let’s understand them in detail before comparing.

Before reading this blog, keep in mind that -3 is bigger than -4 and -3.5 is bigger than -3.51

Math.round()

  • Math.round rounds up to the nearest Integer which can be above or below or even equal to the actual value.
  • In the case of a positive number, anything which is equal to or below x.49 will be converted to a lower number ie x.00 whereas anything which is equal to or more than x.5 will be converted to a higher number ie x+1.00
  • In the case of a negative number, anything which is equal to or below -x.51 will be converted to a lower number ie -x-1.00 whereas anything which is equal to or more than -x.5 will be converted to a higher number ie -x.00
println(Math.round(3.00))
//this will print: 3
println(Math.round(3.49))
//this will print: 3
println(Math.round(3.5))
//this will print: 4
println(Math.round(-3.00))
//this will print: -3
println(Math.round(-3.5))
//this will print: -3
println(Math.round(-3.51))
//this will print: -4

Math.floor()

  • Math.floor rounds up to the nearest Integer which can be equal to or below the actual value.
  • In the case of a positive number, anything between x.01 to x.99 will be converted to a lower number ie x.00 whereas x.00 will remain the same x.00
  • In the case of a negative number, anything between -x.01 to -x.99 will be converted to a lower number ie -x-1.00 whereas -x.00 will remain the same -x.00
println(Math.floor(3.00))
//this will print: 3
println(Math.floor(3.01))
//this will print: 3
println(Math.floor(3.99))
//this will print: 3

println(Math.floor(-3.00))
//this will print: -3
println(Math.floor(-3.01))
//this will print: -4
println(Math.floor(-3.99))
//this will print: -4

Please continue reading at https://agrawalsuneet.github.io/blogs/math-round-vs-math-floor-vs-math-ceil-kotlin/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Talking Aurora at Mesoscon Europe

How to Deploy Cloud Infrastructure with Python

Why Bug Fixing is the Best Way to Start Your Career

Data Engineering for Business Intelligence

Replicating Microsoft News with Xamarin Part 4 (Using Themes)

Difference between a router and a layer

Improve your AWS RDS MySQL Database

Linked List in Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

RetryWithTimes — A helper function build with coroutine

How to write null safe code with Kotlin? — Part 2

Caffeine cache simplified in Kotlin

Organizing dependencies with version catalogs in gradle