Addition for BigDecimal
The BigDecimal is immutable so you need to do this: BigDecimal result = test.add(new BigDecimal(30)); System.out.println(result);
The BigDecimal is immutable so you need to do this: BigDecimal result = test.add(new BigDecimal(30)); System.out.println(result);
The answer is in the JavaDoc of the equals() method: Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). In other words: equals() checks if the BigDecimal objects are exactly the same in every … Read more
Original answer Yes, this is possible: List<BigDecimal> bdList = new ArrayList<>(); //populate list BigDecimal result = bdList.stream() .reduce(BigDecimal.ZERO, BigDecimal::add); What it does is: Obtain a List<BigDecimal>. Turn it into a Stream<BigDecimal> Call the reduce method. 3.1. We supply an identity value for addition, namely BigDecimal.ZERO. 3.2. We specify the BinaryOperator<BigDecimal>, which adds two BigDecimal‘s, via … Read more
You can change the separator either by setting a locale or using the DecimalFormatSymbols. If you want the grouping separator to be a point, you can use an european locale: NumberFormat nf = NumberFormat.getNumberInstance(Locale.GERMAN); DecimalFormat df = (DecimalFormat)nf; Alternatively you can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers … Read more
value = value.setScale(2, RoundingMode.CEILING)
Use compareTo(BigDecimal.ZERO) instead of equals(): if (price.compareTo(BigDecimal.ZERO) == 0) // see below Comparing with the BigDecimal constant BigDecimal.ZERO avoids having to construct a new BigDecimal(0) every execution. FYI, BigDecimal also has constants BigDecimal.ONE and BigDecimal.TEN for your convenience. Note! The reason you can’t use BigDecimal#equals() is that it takes scale into consideration: new BigDecimal(“0”).equals(BigDecimal.ZERO) // … Read more
It’s as simple as: if (value.compareTo(BigDecimal.ZERO) > 0) The documentation for compareTo actually specifies that it will return -1, 0 or 1, but the more general Comparable<T>.compareTo method only guarantees less than zero, zero, or greater than zero for the appropriate three cases – so I typically just stick to that comparison.
A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001) could result in the 0.001 being dropped alltogether when summing as the difference in magnitude is so large. With BigDecimal this would not happen. The disadvantage of BigDecimal is that … Read more
From the Java 11 BigDecimal docs: When a MathContext object is supplied with a precision setting of 0 (for example, MathContext.UNLIMITED), arithmetic operations are exact, as are the arithmetic methods which take no MathContext object. (This is the only behavior that was supported in releases prior to 5.) As a corollary of computing the exact … Read more