Questions tagged [bigdecimal]

BigDecimal is a numeric object type in Java that represents decimal numbers with arbitrary precision.

Because double and float use a fixed amount of memory (64 and 32 bits respectively), they have limited precision which can lead to rounding errors, and more importantly, they cannot be used to represent all decimal fractions exactly as they use two's complement.

BigDecimal solves these problems by providing arbitrary precision and by using a decimal representation of their values.

The BigDecimal class also provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion.

More information can be found in the BigDecimal Javadoc

1565 questions
547
votes
9 answers

ArithmeticException: "Non-terminating decimal expansion; no exact representable decimal result"

Why does the following code raise the exception shown below? BigDecimal a = new BigDecimal("1.6"); BigDecimal b = new BigDecimal("9.2"); a.divide(b) // results in the following exception. Exception: java.lang.ArithmeticException: Non-terminating…
Jason
  • 10,907
  • 18
  • 45
  • 66
343
votes
6 answers

Double vs. BigDecimal?

I have to calculate some floating point variables and my colleague suggest me to use BigDecimal instead of double since it will be more precise. But I want to know what it is and how to make most out of BigDecimal?
Truong Ha
  • 9,070
  • 10
  • 35
  • 45
275
votes
7 answers

Compare if BigDecimal is greater than zero

How can I compare if BigDecimal value is greater than zero?
Santhosh
  • 16,978
  • 21
  • 60
  • 74
233
votes
12 answers

How to check if BigDecimal variable == 0 in java?

I have the following code in Java; BigDecimal price; // assigned elsewhere if (price.compareTo(new BigDecimal("0.00")) == 0) { return true; } What is the best way to write the if condition?
JoJo
  • 3,993
  • 8
  • 36
  • 63
232
votes
1 answer

Rounding BigDecimal to *always* have two decimal places

I'm trying to round BigDecimal values up, to two decimal places. I'm using BigDecimal rounded = value.round(new MathContext(2, RoundingMode.CEILING)); logger.trace("rounded {} to {}", value, rounded); but it doesn't do what I want…
Brad Mace
  • 26,280
  • 15
  • 94
  • 141
216
votes
8 answers

How to change the decimal separator of DecimalFormat from comma to dot/point?

I have this little crazy method that converts BigDecimal values into nice and readable Strings. private String formatBigDecimal(BigDecimal bd){ DecimalFormat df = new DecimalFormat(); df.setMinimumFractionDigits(3); …
MiraFayless
  • 2,223
  • 3
  • 14
  • 8
186
votes
5 answers

Adding up BigDecimals using Streams

I have a collection of BigDecimals (in this example, a LinkedList) that I would like to add together. Is it possible to use streams for this? I noticed the Stream class has several methods Stream::mapToInt Stream::mapToDouble Stream::mapToLong Each…
ryvantage
  • 11,982
  • 13
  • 54
  • 98
172
votes
4 answers

BigDecimal equals() versus compareTo()

Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub …
Buhake Sindi
  • 82,658
  • 26
  • 157
  • 220
162
votes
11 answers

Addition for BigDecimal

I want to do some simple sums with some currency values expressed in BigDecimal type. BigDecimal test = new BigDecimal(0); System.out.println(test); test.add(new BigDecimal(30)); System.out.println(test); test.add(new…
Sergio del Amo
  • 71,609
  • 66
  • 148
  • 177
153
votes
2 answers

BigDecimal setScale and round

What is the difference between this two call? (Is there any?) // 1. new BigDecimal("3.53456").round(new MathContext(4, RoundingMode.HALF_UP)); // 2. new BigDecimal("3.53456").setScale(4, RoundingMode.HALF_UP);
user
  • 5,658
  • 17
  • 52
  • 83
143
votes
11 answers

How to set thousands separator in Java?

How to set thousands separator in Java? I have String representation of a BigDecimal that I want to format with a thousands separator and return as String.
Funtime
  • 2,106
  • 4
  • 18
  • 19
140
votes
8 answers

Converting BigDecimal to Integer

I have Hibernate method which returns me a BigDecimal. I have another API method to which I need to pass that number but it accepts Integer as parameter. I cannot change return types or variable types of both methods. Now how to convert the…
Vishal
  • 2,625
  • 7
  • 29
  • 40
124
votes
9 answers

Safe String to BigDecimal conversion

I'm trying to read some BigDecimal values from the string. Let's say I have this String: "1,000,000,000.999999999999999" and I want to get a BigDecimal out of it. What is the way to do it? First of all, I don't like the solutions using string…
bezmax
  • 23,632
  • 10
  • 46
  • 84
122
votes
20 answers

Convert seconds value to hours minutes seconds?

I've been trying to convert a value of seconds (in a BigDecimal variable) to a string in an editText like "1 hour 22 minutes 33 seconds" or something of the kind. I've tried this: String sequenceCaptureTime = ""; BigDecimal roundThreeCalc = new…
rabbitt
  • 2,278
  • 8
  • 26
  • 40
119
votes
8 answers

How to use comparison operators like >, =, < on BigDecimal

I have a domain class with unitPrice set as BigDecimal data type. Now I am trying to create a method to compare price but it seems like I can't have comparison operators in BigDecimal data type. Do I have to change data type or is there other way…
user3127109
  • 2,401
  • 4
  • 20
  • 33
1
2 3
99 100