Java Utililty Methods BigDecimal

List of utility methods to do BigDecimal

Description

The list of methods to do BigDecimal are organized into topic(s).

Method

BigDecimalgetBtcToBuy(BigDecimal ask, BigDecimal bid, BigDecimal fee, BigDecimal scalpAmount)
Get the amount of BTC that needs to be bought after selling the scalpped amount: x = A*N*(1-F) / B Where: A = Ask N = Scalp amount F = Transaction fees B = Bid
BigDecimal one = new BigDecimal(1);
BigDecimal oneMinusFee = one.subtract(fee);
BigDecimal btcToBuy = ask.multiply(scalpAmount).multiply(oneMinusFee).divide(bid, 8, RoundingMode.FLOOR);
return btcToBuy;
byte[]getByteArrayFromBigDecimalArray(Object value)
Convert an instance of our value class into a byte[].
if (value == null) {
    return null;
BigDecimal[] a = (BigDecimal[]) value;
byte[] total = new byte[a.length * TOTAL_BYTES];
int index = 0;
for (int i = 0; i < a.length; i++) {
    System.arraycopy(new byte[] { (byte) a[i].signum() }, 0, total, index, NR_SIGNAL_BYTES);
...
BigDecimalgetConversionUsdBased(final String destinycode, final BigDecimal amount, final BigDecimal usdBasedBaseRate, BigDecimal usdBasedDestinyRate)
get Conversion Usd Based
if (destinycode.equals("USD")) {
    return amount.multiply(usdBasedDestinyRate);
} else {
    BigDecimal inverted = BigDecimal.ONE.divide(usdBasedBaseRate, 2, RoundingMode.HALF_UP);
    BigDecimal baseRatio = usdBasedDestinyRate.multiply(inverted);
    return amount.multiply(baseRatio);
DategetDateFromRank(BigDecimal rank)
get Date From Rank
return new Date(rank.negate().scaleByPowerOfTen(3).longValue());
BigDecimalgetDepositAmount(Boolean isReceipt, BigDecimal amount)
get Deposit Amount
BigDecimal deposit = BigDecimal.ZERO;
if (isReceipt) {
    if (amount.compareTo(BigDecimal.ZERO) == 1) {
        deposit = amount;
} else {
    if (amount.compareTo(BigDecimal.ZERO) == -1) {
        deposit = amount.abs();
...
intgetDigits(BigDecimal value)
get Digits
String string = value.stripTrailingZeros().toPlainString();
int index = string.indexOf(".");
int scale = index < 0 ? 0 : string.length() - index - 1;
return scale;
doublegetDistance(BigDecimal sourceLatitude, BigDecimal sourceLongitude, BigDecimal destLatitude, BigDecimal destLongitude)
This method takes the source latitude, longitude and destination latitude, longitude to calculate the distance between two points and returns the distance
double distance = 0.0;
double diffOfLat = Math.toRadians(destLatitude.doubleValue() - sourceLatitude.doubleValue());
double diffOfLon = Math.toRadians(destLongitude.doubleValue() - sourceLongitude.doubleValue());
double sourceLatRad = Math.toRadians(sourceLatitude.doubleValue());
double destLatRad = Math.toRadians(destLatitude.doubleValue());
double calcResult = Math.sin(diffOfLat / 2) * Math.sin(diffOfLat / 2)
        + Math.cos(sourceLatRad) * Math.cos(destLatRad) * Math.sin(diffOfLon / 2) * Math.sin(diffOfLon / 2);
calcResult = 2 * Math.atan2(Math.sqrt(calcResult), Math.sqrt(1 - calcResult));
...
BigDecimalgetDividedBigDecimal(final int nDividend, final int nDivisor)
Get the division result using BigDecimal .
final BigDecimal aDividend = BigDecimal.valueOf(nDividend);
final BigDecimal aDivisor = BigDecimal.valueOf(nDivisor);
return aDividend.divide(aDivisor);
doublegetDoubleBigDecimal(String amount, int afterDot)
get Double Big Decimal
double result = 0;
BigDecimal bigDecimal = new BigDecimal(amount);
result = bigDecimal.setScale(afterDot, BigDecimal.ROUND_HALF_EVEN).doubleValue();
return result;
DategetEndTimeByManDay(Date serviceStartTime, BigDecimal manDay)
get End Time By Man Day
if (serviceStartTime == null) {
    return null;
Calendar startTime = Calendar.getInstance();
startTime.setTime(serviceStartTime);
startTime.set(Calendar.MINUTE, 0);
startTime.set(Calendar.SECOND, 0);
startTime.set(Calendar.MILLISECOND, 0);
...