Combined rounding math double

I use ordinary math 3.6.1.

I need to round double value to 2 decimal

Suppose this is my double value:

double d = 400.54540997260267; 

Now, rounding this number, I was expecting 400.54 as the result

Instead, if my number were double, d1 = 400.54640997260267; I was expecting a result of 400.55

I am using this code now:

 Precision.round(d, 2, BigDecimal.ROUND_DOWN); 

If I use BigDecimal.ROUND_DOWN rounding as BigDecimal.ROUND_DOWN I always get the lowest rounding value. What rounding method should I use to get what I expected?

I tried the following code:

 public class TestCalcoli { private static final Logger logger = LoggerFactory.getLogger(TestCalcoli.class.getName()); private void calc(double d) { double result = Precision.round(d, 2, BigDecimal.ROUND_HALF_DOWN); double result2 = Precision.round(d, 2, BigDecimal.ROUND_HALF_UP); logger.info("d--> "+d+" result --> "+result+" result2 --> "+result2); } @Test public void calcola() { try { double d = 400.54540997260267; double d1 = 400.54640997260267; calc(d1); calc(d); } catch (Exception e) { logger.error("errore", e); } } } 

CONSOLE EXIT:

 2017-07-31 09:29:44,608 317 [main] INFO iecrpwb.test.TestCalcoli - d--> 400.54640997260265 result --> 400.55 result2 --> 400.55 2017-07-31 09:29:44,612 321 [main] INFO iecrpwb.test.TestCalcoli - d--> 400.54540997260267 result --> 400.55 result2 --> 400.55 
+5
source share
3 answers

You must use HALF_UP if you want to round the same distance, i.e. the number 5.

+5
source

Here are the available rounding methods appropriate for your task and their respective output for two values:

 Method 400.54540997260267 400.54640997260267 --------------- ------------------ ------------------ ROUND_CEILING 400.55 400.55 ROUND_DOWN 400.54 400.54 ROUND_FLOOR 400.54 400.54 ROUND_HALF_DOWN 400.55 400.55 ROUND_HALF_EVEN 400.55 400.55 ROUND_HALF_UP 400.55 400.55 ROUND_UP 400.55 400.55 

None of the methods give the expected results.

The reason for this is that the two neighbors 400.54540997260267 and 400.54640997260267 are 400.54 and 400.55 . These neighbors are not equidistant from 400.54540997260267 or 400.54640997260267 . Therefore, HALF rounding methods are always rounded to the nearest neighbor, which in both cases is 400.55 .

Based on xenteros answer, the desired results can be achieved using two rounding operations in a row. First round with an extra precision digit, and then perform the desired rounding with the required precision:

 double truncated = Precision.round(input, 3, BigDecimal.ROUND_FLOOR); double rounded = Precision.round(truncated, 2, BigDecimal.ROUND_HALF_DOWN); 
+3
source

The easiest way to achieve what you want is to do the following:

 double d = 400.54540997260267; Double temp = 1000*d; temp = 1.0*temp.intValue()/1000; Precision.round(temp, 2, BigDecimal.HALF_DOWN); 

It removes the digits from the 4th decimal place and then performs the desired rounding.

+1
source

Source: https://habr.com/ru/post/1270382/


All Articles