🌐
Reddit
reddit.com › r/java › double vs bigdecimal in financial programming
r/java on Reddit: Double vs BigDecimal in financial programming
May 22, 2018 -

Everyone says that BigDecimal should be used when dealing with money but it’s much slower and takes more memory than double. I would think this would be especially important in high frequency low-latency applications like trading. Do people actually use BigDecimal in such systems or do they use doubles with some kind of workaround to handle the precision issue?

Edit: I do have experience working on trading and risk systems and I see doubles used much more often than BigDecimal so I was curious to see if this is more common in actual practice. Most of the systems I worked on only need precision to the penny so I wonder if that’s the reason?

Also, BigDecimal is a pain to use and code written with it look much uglier than plain doubles.

Java Double Precision Aug 24, 2020
r/java
6y ago
Handling money in a financial java app? Oct 1, 2017
r/javahelp
9y ago
Decimal Format Sep 13, 2018
r/javahelp
8y ago
Why we don't use doubles for financial calculations Jun 22, 2011
r/programming
15y ago
More results from reddit.com
🌐
Medium
medium.com › javarevisited › java-how-exactly-should-long-and-bigdecimal-be-used-can-you-distinguish-them-5c91c8bce18f
JAVA: How Exactly Should Long and BigDecimal Be Used? Can You Distinguish Them? | by Oliver Foster | Javarevisited | Medium
January 30, 2024 - Range: The range of Long is from -2^63 to 2^63-1. Performance: Compared to BigDecimal, Long typically offers faster performance as it is a wrapper class for a primitive type.
Top answer
1 of 5
9
long can't have decimals. int = 32-bit integer long = 64-bit integer float = 32-bit floating point double = 64-bit floating point If you want to (visually) understand how floating point works, then you should read and experiment here .
2 of 5
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full - best also formatted as code block You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Medium
medium.com › codetutorials › long-vs-bigdecimal-which-should-you-use-for-money-in-java-dfebc5e7488c
Long vs BigDecimal: Which Should You Use for Money in Java? | by Umesh Kumar Yadav | CodeTutorials | Medium
July 18, 2026 - Some developers insist that BigDecimal is the only correct answer. Others argue that Long is faster, simpler, and production-proven.
🌐
LinkedIn
linkedin.com › pulse › need-precision-why-you-should-use-javas-bigdecimal-janaka-wijerathne-vzoic
Need Precision? Why You Should Use Java’s BigDecimal and BigInteger Instead of Primitives
June 19, 2024 - In addition to `BigDecimal`, Java provides the `BigInteger` class for operations involving very large integers that exceed the capacity of primitive data types like `int` and `long`.
🌐
DEV Community
dev.to › luke_tong_d4f228249f32d86 › beyond-double-essential-bigdecimal-practices-for-accurate-financial-calculations-52m1
Beyond double: Essential BigDecimal Practices for Accurate Financial Calculations - DEV Community
May 28, 2025 - Other useful methods include subtractExact(), multiplyExact(), incrementExact(), decrementExact(), and toIntExact() (for converting long to int safely). Using these methods makes your code more robust by failing fast when an overflow occurs. Working with numbers in software, especially when money or critical quantities are involved, requires diligence. BigDecimal is a powerful tool, but like any tool, you need to understand its intricacies.
🌐
LinkedIn
linkedin.com › pulse › why-we-should-use-bigdecimal-instead-double-java-financial-ismail
Why we should use BigDecimal instead double in java for financial systems
May 17, 2022 - This time we got correct outputs for both Example 5.1 & 5.2 . Hence we should always try to use String constructor of BigDecimal class as much as possible where each digit of decimal part are important.
Find elsewhere
🌐
Vanillajava
blog.vanillajava.blog › 2024 › 11 › overview-many-developers-consider.html
Why double Still Outperforms BigDecimal: A Decade-Long Performance Comparison
December 19, 2024 - Does this give you different results? For the most part, double provides 15 digits of precision, which is more than enough for typical monetary values. If these prices had 17 digits, BigDecimal might be more appropriate.
🌐
Reddit
reddit.com › r/programmerhumor › what is big decimal?
r/ProgrammerHumor on Reddit: What is big decimal?
July 2, 2020 - The best way (as far as I know) is to use a complex type of a long int (representing an amount in the smallest currency unit) and something representing the currency (USD for example), and the currency type will have the precision.
🌐
Baeldung
baeldung.com › home › java › java numbers › convert long to bigdecimal in java
Convert Long to BigDecimal in Java | Baeldung
January 8, 2024 - When we talk about converting Long to BigDecimal, some may think both are in the Number type, so we could probably cast Long to BigDecimal like BigDecimal bd = (BigDecimal) longValue.
🌐
Frankel
blog.frankel.ch › bigdecimal-vs-double
double, BigDecimal, or Fixed-Point?
June 14, 2026 - Money: backed by BigDecimal. Maximum precision, full decimal control. FastMoney: backed by long with a fixed scale of 5 decimal places.
🌐
Reddit
reddit.com › r/node › bigdecimal equivalent. what do you use?
r/node on Reddit: BigDecimal equivalent. What do you use?
December 15, 2022 -

I come from the Java world. Worked a lot in fintech. I used BigDecimal all over the place for calculations when it came to money. Is there a library you use for precise numerical calculations?

Top answer
1 of 4
17

From: http://www.javamex.com/tutorials/math/BigDecimal_BigInteger_performance.shtml

Note that a BigDecimal is essentially a wrapper around a BigInteger that "remembers where the decimal point is".

Because of the way we are used to dealing with numbers, as humans, we may have to "think a bit more" when calculating with non-integers by hand compared to integers (e.g. we may have learnt the 7 times table by heart to speed up certain calculations, but probably not the .7 times table). But to a computer implementation, it is essentially no more effort to manipulate non-integers than it is integers, and so on the whole, methods on BigDecimal tend to perform similarly to analogous methods on BigInteger.

2 of 4
4

I know the title of your question states "Performace of BigDecimal vs. BigInteger and BigDecimal" but one of your questions states "How about using only primitive data types and BigInteger or only BigInteger to make ". Well here is what I recommend:

If you need accuracy, precision and you have large numbers to work with such as huge financial data then it may be best to use BigDecimal and BigInteger instead of primitive types. Few other characteristics of BigDecimal and BigInteger are also:

  1. Both are from immutable objects.
  2. Both extend from Number class and implement comparable interface

Now back to your main question in terms of performance, there is not much of difference in terms of performance between the two. If you can present your information in BigDecimal just use that so you don't have to convert it over to BigInteger but again look at the program and see if it does make sense to only use BigDecimal all the time. The main difference between the BigInteger and BigDecimal is that BigInteger supports arbitrary-precision integers and BigDecimal is for arbitrary-precision fixed-point numbers.

Here are my sources and good luck :)

NY University

Colorado University

Top answer
1 of 1
12
  1. What is the difference between BIGINT and LONG on the MySQL level?

    • java.sql.Types.BIGINT is defined as:

      The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type BIGINT.

      Under Integer Types, the MySQL manual documents that its BIGINT datatype is an 8-byte (i.e. 64-bit) integer. The Java primitive datatype long (and its associated wrapper class java.lang.Long) also represents an 8-byte integer.

      Consequently, as documented under Mapping SQL and Java Types:

      The JDBC type BIGINT represents a 64-bit signed integer value between -9223372036854775808 and 9223372036854775807.

      [ deletia — outdated ]

      The recommended Java mapping for the BIGINT type is as a Java long.

    • There is no LONG constant within java.sql.Types, nor does MySQL have a LONG datatype.

    Which is more ideal for storing currencies?

    Neither. Store the dollar amount as a discrete decimal value using the NUMERIC fixed-point datatype. If you insist on storing the cent amount in an integer type, the "correct" answer depends merely on the anticipated range of values that you wish to support.

  2. What would happen if I use PreparedStatement::setLong on a BIGINT column?

    That is exactly how one should set the value of a BIGINT parameter. As documented under java.sql.PreparedStatement::setLong(int, long):

    The driver converts this to an SQL BIGINT value when it sends it to the database.

  3. That also begs the question, what happens if I use PreparedStatement::setBigDecimal on a BIGINT column?

    As documented under java.sql.PreparedStatement::setBigDecimal(int, java.math.BigDecimal):

    The driver converts this to an SQL NUMERIC value when it sends it to the database.

    So, Java will send your value to MySQL as a NUMERIC (fixed-point decimal) datatype. MySQL will then perform type conversion as described under Type Conversion in Expression Evaluation: e.g. if you are simply storing the value in a BIGINT column, the value will be cast to a BIGINT for storage. Provided that the value is in range for a BIGINT, you shouldn't notice any ill-effect (except loss of the fractional part).

🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal and biginteger in java
BigDecimal and BigInteger in Java | Baeldung
December 16, 2024 - This method converts the double to its string representation before converting to BigDecimal, ensuring that we get the expected value every time. Just like the other Number classes (Integer, Long, Double, etc.), BigDecimal provides operations for arithmetic and comparison operations.