

Rounding in Java – Math.Round, Math.Floor, Math.Ceil
source link: https://www.thejavaprogrammer.com/rounding-in-java/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rounding in Java – Math.Round, Math.Floor, Math.Ceil
We have decimal values in java, but sometimes there is a need to round them. It is important to remember which function should be used according to the requirements. So In this blog, I will be sharing some ways to round off decimal values in java.
Checking whether java rounds the value up or down by default.
Output:
Explanation:
We are taking two values val1 and val2 from the user as inputs. Then we are dividing both of them and find whether the result will be rounded up or down. Since we are not using any function so the output will be the default result produced in Java while performing division. As an example the val1 is taken as 15 and val2 is taken as 4. The result which has come is 3, through this observation we can conclude that java rounds down by default.
Using Math.Ceil Function for Rounding Value Up
Output:
Explanation:
We are taking a decimal value from the user as input. The Math.ceil() function is used to round up this decimal value. As an example 45.02 is rounded to 46.0.
Using Math.Floor Function for Rounding Value Down
Output:
Explanation:
We are taking a decimal value from the user as input. The Math.floor() function is used to round down this decimal value. As an example 45.99 is rounded to 45.0.
Using Math.Round Function for Rounding to Nearest Value
Output:
Explanation:
We are taking a decimal value from the user as input. The Math.floor() function is used to round this decimal value to its nearest decimal number. As an example 45.51 is rounded to 46.0. Similarly 45.49, 45.50 will round to 45.0, 46.0.
Conclusion
Do remember that after rounding the value either up or down, the value will still be a decimal number in all the above cases.
So these were the ways of rounding a number in java. Please feel free to ask your doubts in the comment section of this blog.
report this adLeave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Recommend
-
15
SQL Server Rounding Functions - Round, Ceiling and Floor By: Jeremy Kadlec | Updated: 2021-07-20 |
-
7
Java Math.round() with Examples Oct 12, 2019 · 2 mins read The java.lang.M...
-
6
Java Math.floor() Method with Examples Oct 12, 2019 · 2 mins read The java....
-
10
Java Math.ceil() Method with Examples Oct 12, 2019 · 2 mins read The java....
-
6
padfoot1717's blog
-
8
Copy link Contributor clarfonthey
-
12
Mr.Feng BlogNLP、深度学习、机器学习、Python、Gofloor、ceil、round、trunc函数都是什么?日常编程中,你是否也遇到过取值问题时面对这样的函数floor、ceil、ro...
-
9
round-decimal Decimal rounding with ULP. Warning Work in process Installation npm install -D round-decimal
-
6
Benjamin007's blog Ceil and fl...
-
5
PHP RFC: Add 4 new rounding modes to round() function ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK