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

1677 questions
72
votes
9 answers

Using BigDecimal to work with currencies

I was trying to make my own class for currencies using longs, but apparently I should use BigDecimal instead. Could someone help me get started? What would be the best way to use BigDecimals for dollar currencies, like making it at least but no more…
mk12
  • 25,873
  • 32
  • 98
  • 137
71
votes
4 answers

Java, extract just the fractional part of a BigDecimal?

In Java, I'm working with the BigDecimal class and part of my code requires me to extract the fractional part from it. BigDecimal does not appear to have any built in methods to help me get the number after the decimal point of a BigDecimal. For…
Franklin
  • 1,771
  • 3
  • 17
  • 35
70
votes
3 answers

How to multiply a BigDecimal by an integer in Java

How do you multiply a BigDecimal by an integer in Java? I tried this but its not correct. import java.math.BigDecimal; import java.math.MathContext; public class Payment { int itemCost; int totalCost = 0; public BigDecimal…
Adesh
  • 937
  • 4
  • 11
  • 17
68
votes
7 answers

Check if BigDecimal is an integer in Java

What is an efficient way of determining whether a BigDecimal is an integer value in the mathematical sense? At present I have the following code: private boolean isIntegerValue(BigDecimal bd) { boolean ret; try { …
Adamski
  • 54,009
  • 15
  • 113
  • 152
66
votes
19 answers

What to do with Java BigDecimal performance?

I write currency trading applications for living, so I have to work with monetary values (it's a shame that Java still doesn't have decimal float type and has nothing to support arbitrary-precision monetary calculations). "Use BigDecimal!" — you…
Alexander Temerev
  • 661
  • 1
  • 6
  • 5
66
votes
12 answers

Square root of BigDecimal in Java

Can we compute the square root of a BigDecimal in Java by using only the Java API and not a custom-made 100-line algorithm?
user1853200
  • 675
  • 1
  • 5
  • 4
65
votes
4 answers

Convert Java Number to BigDecimal : best way

I am looking for the best way to convert a Number to a BigDecimal. Is this good enough? Number number; BigDecimal big = new BigDecimal(number.toString()); Can we lose precision with the toString() method ?
user1721413
64
votes
3 answers

Why BigDecimal("5.50") not equals to BigDecimal("5.5") and how to work around this issue?

Actually, I've found possible solution //returns true new BigDecimal("5.50").doubleValue() == new BigDecimal("5.5").doubleValue() Of course, it can be improved with something like Math.abs (v1 - v2) < EPS to make the comparison more robust, but…
Roman
  • 64,384
  • 92
  • 238
  • 332
64
votes
7 answers

What is the equivalent of the Java BigDecimal class in C#?

BigDecimal is a class in the java.math package that has a lot of benefits for handling big numbers of a certain scale. Is there an equivalent class or data type in c# with this feature.
Radi
  • 6,548
  • 18
  • 63
  • 91
63
votes
5 answers

Use of java.math.MathContext

Recently I tried understanding the use of java.math.MathContext but failed to understand properly. Is it used for rounding in java.math.BigDecimal. If yes why does not it round the decimal digits but even mantissa part. From API docs, I came to know…
jatanp
  • 3,982
  • 4
  • 40
  • 46
63
votes
6 answers

Java:Why should we use BigDecimal instead of Double in the real world?

When dealing with real world monetary values, I am advised to use BigDecimal instead of Double.But I have not got a convincing explanation except, "It is normally done that way". Can you please throw light on this question?
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
63
votes
4 answers

How can I parse a String to BigDecimal?

I have this String: 10,692,467,440,017.120 (it's an amount). I want to parse it to a BigDecimal. The problem is that I have tried both DecimalFormat and NumbeFormat in vain.
BenMansourNizar
  • 1,558
  • 4
  • 21
  • 42
62
votes
2 answers

How can I divide properly using BigDecimal

My code sample: import java.math.*; public class x { public static void main(String[] args) { BigDecimal a = new BigDecimal("1"); BigDecimal b = new BigDecimal("3"); BigDecimal c = a.divide(b, BigDecimal.ROUND_HALF_UP); …
Jan Ajan
  • 1,461
  • 3
  • 12
  • 15
61
votes
3 answers

Rounding necessary with BigDecimal numbers

I want to set scale of two BigDecimal numbers a and b . as in this example : BigDecimal a = new BigDecimal("2.6E-1095"); BigDecimal b = new BigDecimal("2.7E-1105"); int i = 112, j=1; BigDecimal aa = a.setScale(i+j); …
Mehdi
  • 2,160
  • 6
  • 36
  • 53
60
votes
7 answers

Change DecimalFormat locale

I have custom DecimalFormat in Edittext's addTextChangedListener method, everything is working perfectly but when I change language (locale) my addTextChangedListener is not working. double answer = inputDouble * counterToDouble; DecimalFormat df =…
BekaKK
  • 2,173
  • 6
  • 42
  • 80