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 OverflowThat 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,
Lets make it generic:
public static <T extends Comparable<T>> boolean isBetween(T value, T start, T end) {
return value.compareTo(start) >= 0 && value.compareTo(end) <= 0;
}
class BigDecRand {
public static void main(String[] args) {
String range = args[0];
BigDecimal max = new BigDecimal(range + ".0");
BigDecimal randFromDouble = new BigDecimal(Math.random());
BigDecimal actualRandomDec = randFromDouble.divide(max,BigDecimal.ROUND_DOWN);
BigInteger actualRandom = actualRandomDec.toBigInteger();
}
}
I do this that way
public static BigDecimal generateRandomBigDecimalFromRange(BigDecimal min, BigDecimal max) {
BigDecimal randomBigDecimal = min.add(new BigDecimal(Math.random()).multiply(max.subtract(min)));
return randomBigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP);
}
And the way I run it:
BigDecimal random = Application.generateRandomBigDecimalFromRange(
new BigDecimal(-1.21).setScale(2, BigDecimal.ROUND_HALF_UP),
new BigDecimal(21.28).setScale(2, BigDecimal.ROUND_HALF_UP)
);
Its an arbitrary precision class, it will get as large as you'd like until your computer runs out of memory.
Looking at the source BigDecimal stores it as a BigInteger with a radix,
private BigInteger intVal;
private int scale;
and from BigInteger
/** All integers are stored in 2's-complement form.
63: * If words == null, the ival is the value of this BigInteger.
64: * Otherwise, the first ival elements of words make the value
65: * of this BigInteger, stored in little-endian order, 2's-complement form. */
66: private transient int ival;
67: private transient int[] words;
So the Largest BigDecimal would be,
ival = Integer.MAX_VALUE;
words = new int[Integer.MAX_VALUE];
scale = 0;
You can figure out how to set that. :P
[Edit] So just to calculate that, In binary that's,
(2^35)-2 1's (I think?)
in 2's complement
01111111111111111...until your RAM fills up.
Double.MIN_VALUE represents the minimum positive value. (That is, a positive value close to zero.)
From the documentation:
A constant holding the smallest positive nonzero value of type double, 2-1074.
This is the reason why
System.out.println(inRange(new BigDecimal("0.0")));
System.out.println(inRange(new BigDecimal("-987.0")));
outputs false. None of the provided values are (strictly) greater than 0.
The solution
Since the range of doubles are symmetrical around origo (as opposed to integers, which stretches one step further on the negative side) you can get the minimum (negative) value by writing -Double.MAX_VALUE. That is
BigDecimal DOUBLE_MIN = BigDecimal.valueOf(-Double.MAX_VALUE);
If you see the javadoc for Double.MIN_VALUE it says
A constant holding the smallest **positive nonzero** value of type
<code>double</code>
BigDecimal#toPlainString() will give the decimal part . Note it doesnt exactly satisfy your requirements
If you the op formatted , use DecimalFormat
BigDecimal bd = new BigDecimal("00.0000000000");
System.out.println(bd.toPlainString()); // output = 0.0000000000
As stated by amal you can simply use BigDecimal.toPlainString():
public String toPlainString()
Returns a string representation of this BigDecimal without an exponent field
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 >= 0andprecision - scale -1 >= -6a 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 -1equalsunscaledValue * 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
Precision: Total number of significant digits
Scale: Number of digits to the right of the decimal point
See BigDecimal class documentation for details.