Example usage for java.math BigDecimal compareTo

List of usage examples for java.math BigDecimal compareTo

Introduction

In this page you can find the example usage for java.math BigDecimal compareTo.

Prototype

@Override
public int compareTo(BigDecimal val) 

Source Link

Document

Compares this BigDecimal with the specified BigDecimal .

Usage

From source file:jp.co.ctc_g.jse.core.validation.util.Validators.java

/**
 * ?????????????????????//from www. j av a 2s . c o  m
 * @param suspect 
 * @param target ?
 * @return ???????????false
 */
public static boolean greaterThanEqualsTo(Number suspect, BigDecimal target) {
    if (suspect == null)
        return true;
    BigDecimal number = toBigDecimal(suspect);
    return number.compareTo(target) >= 0;
}

From source file:jp.co.ctc_g.jse.core.validation.util.Validators.java

/**
 * ??????????????//www. ja va2 s  .c o m
 * @param suspect 
 * @param target ?
 * @return ???????false
 */
public static boolean lessThan(Number suspect, BigDecimal target) {
    if (suspect == null)
        return true;
    BigDecimal number = toBigDecimal(suspect);
    return number.compareTo(target) <= -1;
}

From source file:jp.co.ctc_g.jse.core.validation.util.Validators.java

/**
 * ?????????????????????/*from  www . j a  va2 s  .c  o m*/
 * @param suspect 
 * @param target ?
 * @return ???????????false
 */
public static boolean lessThanEqualsTo(Number suspect, BigDecimal target) {
    if (suspect == null)
        return true;
    BigDecimal number = toBigDecimal(suspect);
    return number.compareTo(target) <= 0;
}

From source file:adalid.core.primitives.NumericPrimitive.java

private static int compare(Number x, Number y) {
    BigDecimal bx = new BigDecimal(x.toString());
    BigDecimal by = new BigDecimal(y.toString());
    return bx.compareTo(by);
}

From source file:de.csdev.ebus.command.EBusCommandUtils.java

/**
 * Apply all post number operations like multiply, range check etc.
 *
 * @param decode/*  ww w  .j  a  v  a 2  s  . c  o m*/
 * @param ev
 * @return
 */
private static Object applyNumberOperations(Object decode, IEBusValue ev) {

    if (ev instanceof EBusCommandValue) {
        EBusCommandValue nev = (EBusCommandValue) ev;

        if (decode instanceof BigDecimal) {

            BigDecimal multiply = (BigDecimal) decode;

            if (nev.getFactor() != null) {
                multiply = multiply.multiply(nev.getFactor());
                decode = multiply;
            }

            if (nev.getMin() != null && multiply.compareTo(nev.getMin()) == -1) {
                logger.debug("Value {} with {} is smaller then allowed {}", ev.getName(), multiply,
                        nev.getMax());
                decode = null;
            }

            if (nev.getMax() != null && multiply.compareTo(nev.getMax()) == 1) {
                logger.debug("Value {} with {} is larger then allowed {}", ev.getName(), multiply,
                        nev.getMax());
                decode = null;
            }
        }
    }

    return decode;
}

From source file:com.liato.bankdroid.appwidget.AutoRefreshService.java

public static void showNotification(final Bank bank, final Account account, final BigDecimal diff,
        Context context) {/*from   w ww . j  a v a2 s  .co m*/

    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    if (!prefs.getBoolean("notify_on_change", true)) {
        return;
    }

    String text = String.format("%s: %s%s", account.getName(),
            ((diff.compareTo(new BigDecimal(0)) == 1) ? "+" : ""),
            Helpers.formatBalance(diff, account.getCurrency()));
    if (!prefs.getBoolean("notify_delta_only", false)) {
        text = String.format("%s (%s)", text,
                Helpers.formatBalance(account.getBalance(), account.getCurrency()));
    }

    final NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(NOTIFICATION_SERVICE);
    final NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
            .setSmallIcon(bank.getImageResource()).setContentTitle(bank.getDisplayName()).setContentText(text);

    // Remove notification from statusbar when clicked
    notification.setAutoCancel(true);

    // http://www.freesound.org/samplesViewSingle.php?id=75235
    // http://www.freesound.org/samplesViewSingle.php?id=91924
    if (prefs.getString("notification_sound", null) != null) {
        notification.setSound(Uri.parse(prefs.getString("notification_sound", null)));
    }
    if (prefs.getBoolean("notify_with_vibration", true)) {
        final long[] vib = { 0, 90, 130, 80, 350, 190, 20, 380 };
        notification.setVibrate(vib);
    }

    if (prefs.getBoolean("notify_with_led", true)) {
        notification.setLights(prefs.getInt("notify_with_led_color",
                context.getResources().getColor(R.color.default_led_color)), 700, 200);
    }

    final PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
            new Intent(context, MainActivity.class), 0);
    notification.setContentIntent(contentIntent);

    String numNotifications = prefs.getString("num_notifications", "total");
    int notificationId = (int) (numNotifications.equals("total") ? 0
            : numNotifications.equals("bank") ? bank.getDbId()
                    : numNotifications.equals("account") ? account.getId().hashCode()
                            : SystemClock.elapsedRealtime());
    notificationManager.notify(notificationId, notification.build());

    // Broadcast to Remote Notifier if enabled
    // http://code.google.com/p/android-notifier/
    if (prefs.getBoolean("notify_remotenotifier", false)) {
        final Intent i = new Intent(BROADCAST_REMOTE_NOTIFIER);
        i.putExtra("title", String.format("%s (%s)", bank.getName(), bank.getDisplayName()));
        i.putExtra("description", text);
        context.sendBroadcast(i);
    }

    // Broadcast to OpenWatch if enabled
    // http://forum.xda-developers.com/showthread.php?t=554551
    if (prefs.getBoolean("notify_openwatch", false)) {
        Intent i;
        if (prefs.getBoolean("notify_openwatch_vibrate", false)) {
            i = new Intent(BROADCAST_OPENWATCH_VIBRATE);
        } else {
            i = new Intent(BROADCAST_OPENWATCH_TEXT);
        }
        i.putExtra("line1", String.format("%s (%s)", bank.getName(), bank.getDisplayName()));
        i.putExtra("line2", text);
        context.sendBroadcast(i);
    }

    // Broadcast to LiveView if enabled
    // http://www.sonyericsson.com/cws/products/accessories/overview/liveviewmicrodisplay
    if (prefs.getBoolean("notify_liveview", false)) {
        final Intent i = new Intent(context, LiveViewService.class);
        i.putExtra(LiveViewService.INTENT_EXTRA_ANNOUNCE, true);
        i.putExtra(LiveViewService.INTENT_EXTRA_TITLE,
                String.format("%s (%s)", bank.getName(), bank.getDisplayName()));
        i.putExtra(LiveViewService.INTENT_EXTRA_TEXT, text);
        context.startService(i);
    }

}

From source file:Main.java

/**
 * Compute e^x to a given scale by the Taylor series.
 * @param x the value of x//from  w  ww  . j  a  va2 s .c o  m
 * @param scale the desired scale of the result
 * @return the result value
 */
private static BigDecimal expTaylor(BigDecimal x, int scale) {
    BigDecimal factorial = BigDecimal.valueOf(1);
    BigDecimal xPower = x;
    BigDecimal sumPrev;

    // 1 + x
    BigDecimal sum = x.add(BigDecimal.valueOf(1));

    // Loop until the sums converge
    // (two successive sums are equal after rounding).
    int i = 2;
    do {
        // x^i
        xPower = xPower.multiply(x).setScale(scale, BigDecimal.ROUND_HALF_EVEN);

        // i!
        factorial = factorial.multiply(BigDecimal.valueOf(i));

        // x^i/i!
        BigDecimal term = xPower.divide(factorial, scale, BigDecimal.ROUND_HALF_EVEN);

        // sum = sum + x^i/i!
        sumPrev = sum;
        sum = sum.add(term);

        ++i;
        Thread.yield();
    } while (sum.compareTo(sumPrev) != 0);

    return sum;
}

From source file:ips1ap101.lib.core.util.STP.java

public static boolean esObjetoEnRango(Object objeto, Object minimo, Object maximo) {
    boolean es = true;
    TipoDatoParEnumeration tipo;//from w  w  w .  j av a2s .c  o m
    if (objeto == null) {
        return false;
    } else if (objeto instanceof String) {
        tipo = TipoDatoParEnumeration.ALFANUMERICO;
    } else if (objeto instanceof BigDecimal) {
        tipo = TipoDatoParEnumeration.NUMERICO;
    } else if (objeto instanceof Timestamp) {
        tipo = TipoDatoParEnumeration.FECHA_HORA;
    } else if (objeto instanceof Integer) {
        tipo = TipoDatoParEnumeration.ENTERO;
    } else if (objeto instanceof Long) {
        tipo = TipoDatoParEnumeration.ENTERO_GRANDE;
    } else if (objeto instanceof BigInteger) {
        tipo = TipoDatoParEnumeration.ENTERO_GRANDE;
    } else {
        return false;
    }
    switch (tipo) {
    case ALFANUMERICO:
        String val1 = (String) objeto;
        String min1 = (String) minimo;
        String max1 = (String) maximo;
        if (min1 != null && val1.compareTo(min1) < 0) {
            es = false;
        }
        if (max1 != null && val1.compareTo(max1) > 0) {
            es = false;
        }
        break;
    case NUMERICO:
        BigDecimal val2 = (BigDecimal) objeto;
        BigDecimal min2 = (BigDecimal) minimo;
        BigDecimal max2 = (BigDecimal) maximo;
        if (min2 != null && val2.compareTo(min2) < 0) {
            es = false;
        }
        if (max2 != null && val2.compareTo(max2) > 0) {
            es = false;
        }
        break;
    case FECHA_HORA:
        Timestamp val3 = (Timestamp) objeto;
        Timestamp min3 = (Timestamp) minimo;
        Timestamp max3 = (Timestamp) maximo;
        if (min3 != null && val3.compareTo(min3) < 0) {
            es = false;
        }
        if (max3 != null && val3.compareTo(max3) > 0) {
            es = false;
        }
        break;
    case ENTERO:
        Integer val4 = (Integer) objeto;
        Integer min4 = (Integer) minimo;
        Integer max4 = (Integer) maximo;
        if (min4 != null && val4.compareTo(min4) < 0) {
            es = false;
        }
        if (max4 != null && val4.compareTo(max4) > 0) {
            es = false;
        }
        break;
    case ENTERO_GRANDE:
        Long val5 = objeto instanceof BigInteger ? ((BigInteger) objeto).longValue() : (Long) objeto;
        Long min5 = (Long) minimo;
        Long max5 = (Long) maximo;
        if (min5 != null && val5.compareTo(min5) < 0) {
            es = false;
        }
        if (max5 != null && val5.compareTo(max5) > 0) {
            es = false;
        }
        break;
    }
    return es;
}

From source file:Main.java

/**
 * Compute the natural logarithm of x to a given scale, x > 0.
 * Use Newton's algorithm./*from w w  w.  ja  va2s. c  om*/
 */
private static BigDecimal lnNewton(BigDecimal x, int scale) {
    int sp1 = scale + 1;
    BigDecimal n = x;
    BigDecimal term;

    // Convergence tolerance = 5*(10^-(scale+1))
    BigDecimal tolerance = BigDecimal.valueOf(5).movePointLeft(sp1);

    // Loop until the approximations converge
    // (two successive approximations are within the tolerance).
    do {

        // e^x
        BigDecimal eToX = exp(x, sp1);

        // (e^x - n)/e^x
        term = eToX.subtract(n).divide(eToX, sp1, BigDecimal.ROUND_DOWN);

        // x - (e^x - n)/e^x
        x = x.subtract(term);

        Thread.yield();
    } while (term.compareTo(tolerance) > 0);

    return x.setScale(scale, BigDecimal.ROUND_HALF_EVEN);
}

From source file:com.egt.core.util.STP.java

public static boolean esObjetoEnRango(Object objeto, Object minimo, Object maximo) {
    boolean es = true;
    EnumTipoDatoPar tipo;//from   ww  w . j a  v  a  2s.  co  m
    if (objeto == null) {
        return false;
    } else if (objeto instanceof String) {
        tipo = EnumTipoDatoPar.ALFANUMERICO;
    } else if (objeto instanceof BigDecimal) {
        tipo = EnumTipoDatoPar.NUMERICO;
    } else if (objeto instanceof Timestamp) {
        tipo = EnumTipoDatoPar.FECHA_HORA;
    } else if (objeto instanceof Integer) {
        tipo = EnumTipoDatoPar.ENTERO;
    } else if (objeto instanceof Long) {
        tipo = EnumTipoDatoPar.ENTERO_GRANDE;
    } else if (objeto instanceof BigInteger) {
        tipo = EnumTipoDatoPar.ENTERO_GRANDE;
    } else {
        return false;
    }
    switch (tipo) {
    case ALFANUMERICO:
        String val1 = (String) objeto;
        String min1 = (String) minimo;
        String max1 = (String) maximo;
        if (min1 != null && val1.compareTo(min1) < 0) {
            es = false;
        }
        if (max1 != null && val1.compareTo(max1) > 0) {
            es = false;
        }
        break;
    case NUMERICO:
        BigDecimal val2 = (BigDecimal) objeto;
        BigDecimal min2 = (BigDecimal) minimo;
        BigDecimal max2 = (BigDecimal) maximo;
        if (min2 != null && val2.compareTo(min2) < 0) {
            es = false;
        }
        if (max2 != null && val2.compareTo(max2) > 0) {
            es = false;
        }
        break;
    case FECHA_HORA:
        Timestamp val3 = (Timestamp) objeto;
        Timestamp min3 = (Timestamp) minimo;
        Timestamp max3 = (Timestamp) maximo;
        if (min3 != null && val3.compareTo(min3) < 0) {
            es = false;
        }
        if (max3 != null && val3.compareTo(max3) > 0) {
            es = false;
        }
        break;
    case ENTERO:
        Integer val4 = (Integer) objeto;
        Integer min4 = (Integer) minimo;
        Integer max4 = (Integer) maximo;
        if (min4 != null && val4.compareTo(min4) < 0) {
            es = false;
        }
        if (max4 != null && val4.compareTo(max4) > 0) {
            es = false;
        }
        break;
    case ENTERO_GRANDE:
        Long val5 = objeto instanceof BigInteger ? ((BigInteger) objeto).longValue() : (Long) objeto;
        Long min5 = (Long) minimo;
        Long max5 = (Long) maximo;
        if (min5 != null && val5.compareTo(min5) < 0) {
            es = false;
        }
        if (max5 != null && val5.compareTo(max5) > 0) {
            es = false;
        }
        break;
    }
    return es;
}