Introduction
In this article, we are going to describe a special behavior of numerical operations capable of discarding precision. In each, the rounding mode indicates how the least significant digit of a rounded result is to be calculated. If a fewer number of significant digits are returned then the digits needed to exact numerical result, and the discarded digits will be referred to as the discard function regardless of the digit's contribution to the value of the number.
Each rounding mode description includes a table listing how different two-digit decimal values would round to a one-digit decimal value under the rounding mode in question. The result column in the tables could be determined by creating a BigDecimal number with the specified value, forming a MathContext object with the proper settings (precision set to 1, and the roundingMode set to the rounding mode in question), and calling round on this number with the proper MathContext. A summary table showing the results of these rounding operations for all rounding modes appears below.
There are some predefined constants and their meaning are listed below
1: UP
In this Rounding mode, the numerical value is always rounded to zero. And in this mode the current magnitude is never decreased; any numerical value is always increased.
Example
| Input Number | Input rounded to one digit |
| 7.5 | 8 |
| 1.5 | 2 |
| 3.6 | 4 |
| 3.1 | 4 |
| 3.0 | 3 |
| -3.0 | -3 |
| -3.1 | -4 |
| -3.6 | -4 |
| -3.5 | -4 |
| -7.5 | -8 |
2: DOWN
In this Rounding mode, the numerical value rounds toward zero. And never incremented in the magnitude of calculated numerical value. The digit is never incremented before a discarded fraction.
Example
| Input Number | Input rounded to one digit |
| 7.5 | 7 |
| 1.5 | 1 |
| 3.6 | 3 |
| 3.1 | 3 |
| 3.0 | 3 |
| -3.0 | -3 |
| -3.1 | -3 |
| -3.6 | -3 |
| -3.5 | -3 |
| -7.5 | -7 |
3: CEILING
In this rounding mode, the behavior of CEILING is different for positive and negative integers. If the result is positive, then it behaves as for RoundingMode.UP; if negative, behaves as for RoundingMode.DOWN. But in this rounding mode, the calculated values are never decreased.
Example
| Input Number | Input rounded to one digit |
| 7.5 | 8 |
| 1.5 | 2 |
| 3.6 | 4 |
| 3.1 | 4 |
| 3.0 | 3 |
| -3.0 | -3 |
| -3.1 | -3 |
| -3.6 | -3 |
| -3.5 | -3 |
| -7.5 | -7 |
4: FLOOR
This rounding mode rounds towards negative infinity. The numerical value of the calculated result is less than the absolute calculated value. If the result is positive, then it behaves as for RoundingMode.DOWN; if negative, it behaves as for RoundingMode.UP.
Example
| Input Number | Input rounded to one digit |
| 7.5 | 7 |
| 1.5 | 1 |
| 3.6 | 3 |
| 3.1 | 3 |
| 3.0 | 3 |
| -3.0 | -3 |
| -3.1 | -4 |
| -3.6 | -3 |
| -3.5 | -4 |
| -7.5 | -8 |
5: HALF_UP
In this rounding mode, the rounding is towards the "nearest neighbor" unless both neighbors are equidistant, in which case round up. Behaves as for RoundingMode.UP if the discarded fraction is >= 0.5; otherwise, behaves as for RoundingMode.DOWN. In the case of HALF_UP, both are possible and the calculated result may be less or greater.
Example


Join the conversation! Your thoughts help the community grow.