That is achievable using the .compareTo() method. For instance:

if ( price.compareTo( BigDecimal.valueOf( 500 ) > 0 
     && price.compareTo( BigDecimal.valueOf( 1000 ) < 0 ) {
    // price is larger than 500 and less than 1000
    ...
}

Quoting (and paraphrasing) from the JavaDoc:

The suggested idiom for performing these comparisons is: (x.compareTo(y) op 0), where op is one of the six comparison operators [(<, ==, >, >=, !=, <=)]

Cheers,

Answer from Anders R. Bystrup on Stack Overflow
🌐
CodeGym
codegym.cc › java blog › java numbers › bigdecimal in java
BigDecimal in Java
January 15, 2025 - For example, human life may depend on the accuracy of calculations in some programs (e.g. software that controls airplanes, rockets, or medical equipment). So if the 150th decimal place is important, then BigDecimal is the best choice.
🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal and biginteger in java
BigDecimal and BigInteger in Java | Baeldung
December 16, 2024 - For example, the BigDecimal 3.14 has an unscaled value of 314 and a scale of 2.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
July 21, 2026 - ArithmeticException - if n is out of range. ... Returns a BigDecimal whose value is (thisn). The current implementation uses the core algorithm defined in ANSI standard X3.274-1996 with rounding according to the context settings. In general, the returned numerical value is within two ulps of the exact numerical value for the chosen precision.
🌐
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 - Most operations accept as input one or more values of a given format and produce a result in the same format. A BigDecimal's scale is equivalent to negating an IEEE 754 value's exponent. BigDecimal values do not have a format in the same sense; all values have the same possible range of scale/exponent and the unscaled value has arbitrary precision.
🌐
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 - BigDecimal uses two parameters to define the maximum number of digits it can hold and how many digits are behind the decimal point.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-class-java
BigDecimal Class in Java - GeeksforGeeks
March 8, 2024 - A BigDecimal consists of a random precision integer unscaled value and a 32-bit integer scale. If greater than or equal to zero, the scale is the number of digits to the right of the decimal point.
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 12788821 › return-a-range-to-compare-with-bigdecimal
java - Return a range to compare with BigDecimal - Stack Overflow
April 28, 2016 - In my code a user enters in a specified temperature range with this method (The default range is 0 - 100): public class range { public void rangeset () { int range1 = 0; ...
🌐
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 - In Java, BigDecimal is a class designed to handle arbitrary-precision decimal numbers. Unlike the primitive data types float and double, which have fixed precision, BigDecimal can represent numbers with any desired level of accuracy.
🌐
Stack Overflow
stackoverflow.com › questions › 48540112 › how-to-validate-bigdecimal-in-a-range
java - how to validate BigDecimal in a range? - Stack Overflow
I want to validate a bigDecimal with max 17 integers and 2 fractions, I used @Digits(integer=17, fraction=2) but this does not accept 1.10 for example, and @DecimalMin("0.00") @DecimalMax("99999999999999999.99") accepts for example 10.1234.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
ArithmeticException - if n is out of range. ... Returns a BigDecimal whose value is (thisn). The current implementation uses the core algorithm defined in ANSI standard X3.274-1996 with rounding according to the context settings. In general, the returned numerical value is within two ulps of the exact numerical value for the chosen precision.
🌐
GitHub
github.com › eobermuhlner › big-math
GitHub - eobermuhlner/big-math: Advanced Java BigDecimal math functions (pow, sqrt, log, sin, ...) using arbitrary precision. · GitHub
The class BigDecimalStream provides factory methods for streams of BigDecimal elements. Overloaded variants of range(start, end, step) provide sequential elements equivalent to IntStream.range(start, end) but with configurable step (exclusive the end value).
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal
BigDecimal Class (Java.Math) | Microsoft Learn
As a 32-bit integer, the set of values for the scale is large, but bounded. If the scale of a result would exceed the range of a 32-bit integer, either by overflow or underflow, the operation may throw an ArithmeticException.
🌐
Madplay
madplay.github.io › java/kotlin › java bigdecimal: accurate real numbers and floating point
Java BigDecimal: Accurate Real Numbers and Floating Point | MadPlay🚀
February 17, 2021 - Like integer types int and long, they use 4 and 8 bytes. But based on floating-point representation (splitting into mantissa and exponent), they represent a wider range than integers.
Top answer
1 of 6
181

A BigDecimal is defined by two values: an arbitrary precision integer and a 32-bit integer scale. The value of the BigDecimal is defined to be unscaledValue * 10 ^ (-scale).

Precision:

The precision is the number of digits in the unscaled value. For instance, for the number 123.45, the precision returned is 5.

So, precision indicates the length of the arbitrary precision integer. Here are a few examples of numbers with the same scale, but different precision:

  • 12345 / 100000 = 0.12345 // scale = 5, precision = 5
  • 12340 / 100000 = 0.1234 // scale = 4, precision = 4
  • 1 / 100000 = 0.00001 // scale = 5, precision = 1

In the special case that the number is equal to zero (i.e. 0.000), the precision is always 1.

Scale:

If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. For example, a scale of -3 means the unscaled value is multiplied by 1000.

This means that the integer value of the ‘BigDecimal’ is multiplied by 10 ^ (-scale).

Here are a few examples of the same precision, with different scales:

  • 12345 with scale 5 = 0.12345
  • 12345 with scale 4 = 1.2345
  • …
  • 12345 with scale 0 = 12345
  • 12345 with scale -1 = 123450 †

BigDecimal.toString:

The toString method for a BigDecimal behaves differently based on the scale and precision. (Thanks to @RudyVelthuis for pointing this out.)

  • If scale == 0, the integer is just printed out, as-is.
  • If scale < 0, E-Notation is always used (e.g. 5 scale -1 produces "5E+1")
  • If scale >= 0 and precision - scale -1 >= -6 a plain decimal number is produced (e.g. 10000000 scale 1 produces "1000000.0")
  • Otherwise, E-notation is used, e.g. 10 scale 8 produces "1.0E-7" since precision - scale -1 equals unscaledValue * 10 ^ (-scale) is less than -6.

More examples:

  • 19/100 = 0.19 // integer=19, scale=2, precision=2
  • 1/1000 = 0.0001 // integer=1, scale = 4, precision = 1
2 of 6
98
  • Precision: Total number of significant digits

  • Scale: Number of digits to the right of the decimal point

See BigDecimal class documentation for details.