🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-class-java
BigDecimal Class in Java - GeeksforGeeks
March 8, 2024 - The BigDecimal class provides operations on double numbers for arithmetic, scale handling, rounding, comparison, format conversion and hashing. It can handle very large and very small floating point numbers with great precision but compensating ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point.
🌐
CodeGym
codegym.cc › java blog › java numbers › bigdecimal in java
BigDecimal in Java
January 15, 2025 - What do we need to come up with if our Very Large Number requires 100 bits? Fortunately, we don't need to invent anything. For cases such as this, Java has two special classes: BigInteger (for integers) and BigDecimal (for floating-point numbers).
🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal and biginteger in java
BigDecimal and BigInteger in Java | Baeldung
December 16, 2024 - BigDecimal is a special class in Java used to represent numbers with a high degree of precision.
🌐
Codementor
codementor.io › community › bigdecimal class in java
BigDecimal Class in Java | Codementor
June 23, 2024 - You'll also learn some operations ... traditional Double class. According to its Javadoc, a BigDecimal represents an immutable, arbitrary-precision decimal number....
🌐
Medium
solutionsarchitecture.medium.com › mastering-big-decimals-in-java-understanding-implementation-performance-and-alternatives-7e3a3a96efc6
Mastering Big Decimals in Java: Understanding Implementation, Performance, and Alternatives | by Rahul Krishnan | Medium
September 22, 2024 - How does it work?BigDecimal is implemented using a combination of integer arithmetic and scaling. The number is represented as an integer and a scale, which determines the number of digits to the right of the decimal point.
🌐
Scaler
scaler.com › home › topics › bigdecimal java
BigDecimal Java - Scaler Topics
May 4, 2023 - The BigDecimal class in Java consists of a 32-bit integer scale and a random precision integer scale. If the scale is positive or zero, the number of digits to the right of the decimal point is used.
🌐
Piotr Horzycki
peterdev.pl › all-you-need-to-know-about-javas-bigdecimal
All you need to know about Java’s BigDecimal | Piotr Horzycki - Java and PHP developer’s blog
February 11, 2021 - A very important concept of Java BigDecimal type is immutability. It means that once an object is instantiated, its state cannot be changed.
🌐
IONOS
ionos.com › digital guide › websites › web development › java class: bigdecimal
How to use BigDecimal in Java - IONOS
July 26, 2024 - Java BigDec­i­mal consists of a 32-bit integer scale and an unscaled integer value with optional precision. In this case, “scale” means the number of digits after the decimal point, provided they’re greater than or equal to zero.
Find elsewhere
🌐
DEV Community
dev.to › mohamad_mhana › bigdecimal-in-java-mastering-precision-from-beginner-to-advanced-4273
💰 BigDecimal in Java: Mastering Precision from Beginner to Advanced 🛠️ - DEV Community
October 2, 2025 - In this post, we’ll go from the basics of BigDecimal to advanced tips, complete with examples and best practices. ... BigDecimal stores numbers as exact decimal values, avoiding these issues. Key Takeaway: For financial apps or precise calculations, always consider BigDecimal. ... import java.math.BigDecimal; BigDecimal bd1 = new BigDecimal("0.1"); // Preferred for precision BigDecimal bd2 = BigDecimal.valueOf(0.2); // Safe alternative
🌐
Java67
java67.com › 2020 › 03 › simple-example-of-bigdecimal-in-java.html
How to use BigDecimal in Java? Example | Java67
BigDecimal is used to represent bigger numbers in Java, similar to float and long but many Java programmers also use BigDecimal to store floating-point values because it's easier to perform arithmetic operations on BigDecimal than float or double.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › math
Java BigDecimal Example - Java Code Geeks
May 7, 2021 - This primitive data type in java consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point.
🌐
Medium
pasankamburugamuwa1.medium.com › big-decimal-in-java-2b707086c825
Big Decimal in Java. One of the solutions to a… | by Pasan Kamburugamuwa | Medium
October 5, 2021 - Big Decimal in Java One of the solutions to a floating-point error in java is Big decimal. We can define the BigDecimal as an arbitrary-precision signed immutable number. This BigDecimal has two …
🌐
Baeldung
baeldung.com › home › java › java string › converting string to bigdecimal in java
Converting String to BigDecimal in Java | Baeldung
June 17, 2026 - The BigDecimal class in Java provides operations for basic arithmetic, scale manipulation, comparison, format conversion, and hashing.
Top answer
1 of 4
19

You should use BigDecimal - but use the String constructor, e.g.:

new BigDecimal("10364055.81");

If you pass a double to BigDecimal, Java must create that double first - and since doubles cannot represent most decimal fractions accurately, it does create the value as 10364055.81000000052154064178466796875 and then passes it to the BigDecimal constructor. In this case BigDecimal has no way of knowing that you actually meant the rounder version.

Generally speaking, using non-String constructors of BigDecimal should be considered a warning that you're not getting the full benefit of the class.

Edit - based on rereading exactly what you wanted to do, my initial claim is probably too strong. BigDecimal is a good choice when you need to represent decimal values exactly (money handling being the obvious choice, you don't want 5.99 * one million to be 5990016.45 for example.

But if you're not worried about the number being stored internally as a very slightly different value to the decimal literal you entered, and just want to print it out again in the same format, then as others have said, an instance of NumberFormat (in this case, new DecimalFormat("########.##")) will do the trick to output the double nicely, or String.format can do much the same thing.

As for performance - BigDecimals will naturally be slower than using primitives. Typically, though, unless the vast majority of your program involves mathematical manipulations, you're unlikely to actually notice any speed difference. That's not to say you should use BigDecimals all over; but rather, that if you can get a real benefit from their features that would be difficult or impossible to realise with plain doubles, then don't sweat the miniscule performance difference they theoretically introduce.

2 of 4
5

How a number is displayed is distinct from how the number is stored.

Take a look at DecimalFormat for controlling how you can display your numbers when a double (or float etc.).

Note that choosing BigDecimal over double (or vice versa) has pros/cons, and will depend on your requirements. See here for more info. From the summary:

In summary, if raw performance and space are the most important factors, primitive floating-point types are appropriate. If decimal values need to be represented exactly, high-precision computation is needed, or fine control of rounding is desired, only BigDecimal has the needed capabilities.

🌐
Dummies
dummies.com › article › technology › programming-web-design › java › how-to-create-bigdecimal-objects-in-java-153270
How to Create BigDecimal Objects in Java | dummies
March 26, 2016 - If the initial value is an integer, you can safely pass it to the constructor. Remember that integers don’t have the same accuracy problems that doubles and floats do. Also, you can convert a BigDecimal to a double solely for the purpose of using the NumberFormat class to format the result.
Author:
🌐
Tutorialspoint
tutorialspoint.com › java › math › java_math_bigdecimal.htm
Java.math.BigDecimal Class
The java.math.BigDecimal class provides operations for arithmetic, scale manipulation, rounding, comparison, hashing, and format conversion. The toString() method provides a canonical representation of a BigDecimal.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 17 & JDK 17)
April 21, 2026 - Converts this BigDecimal to an int. This conversion is analogous to the narrowing primitive conversion from double to short as defined in The Java Language Specification: any fractional part of this BigDecimal will be discarded, and if the resulting "BigInteger" is too big to fit in an int, only the low-order 32 bits are returned.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal
BigDecimal Class (Java.Math) | Microsoft Learn
The BigDecimal class gives its user complete control over rounding behavior. If no rounding mode is specified and the exact result cannot be represented, an ArithmeticException is thrown; otherwise, calculations can be carried out to a chosen precision and rounding mode by supplying an appropriate MathContext object to the operation. In either case, eight <em>rounding modes</em> are provided for the control of rounding.