List of usage examples for java.math BigDecimal scale
int scale
To view the source code for java.math BigDecimal scale.
Click Source Link
From source file:Main.java
public static void main(String... args) { long base = 12345; int scale = 4; BigDecimal number = BigDecimal.valueOf(base, scale); System.out.println(number);/*w w w. j a v a2 s.com*/ BigDecimal pointRight = number.movePointRight(5); System.out.println(pointRight + "; my scale is " + pointRight.scale()); BigDecimal scaleBy = number.scaleByPowerOfTen(5); System.out.println(scaleBy + "; my scale is " + scaleBy.scale()); }
From source file:Main.java
public static void main(String[] args) { BigDecimal bg1 = new BigDecimal("123.0"); BigDecimal bg2 = new BigDecimal("-1.123"); // assign the result of scale on bg1, bg2 to i1,i2 int i1 = bg1.scale(); int i2 = bg2.scale(); String str1 = "The scale of " + bg1 + " is " + i1; String str2 = "The scale of " + bg2 + " is " + i2; // print the values of i1,i2; System.out.println(str1);/* www . j a v a2s.c om*/ System.out.println(str2); }
From source file:org.cryptomath.util.NumberUtil.java
public static BigDecimal truncate(final String text, final int pTimes) { BigDecimal bigDecimal = new BigDecimal(text); if (bigDecimal.scale() > pTimes) { bigDecimal = new BigDecimal(text).setScale(pTimes, RoundingMode.HALF_UP); }//from www. ja v a2s .c o m return bigDecimal.stripTrailingZeros(); }
From source file:Main.java
/** * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java *///from w w w . j a v a 2 s.co m public static long parseMoney(String money) { if (money != null) { BigDecimal bdMoney; money = money.trim(); // to be safe try { bdMoney = new BigDecimal(money); return moneyAsLong(bdMoney); } catch (NumberFormatException e) { /* there must be commas, etc in the number. Need to look for them * and remove them first, and then try BigDecimal again. If that * fails, then give up and use NumberFormat and scale it down * */ String[] split = MONEY_PREFIX_PATTERN.split(money); if (split.length > 1) { StringBuilder buf = new StringBuilder(); if (money.startsWith("-")) { buf.append('-'); } for (int i = 0; i < split.length - 1; i++) { buf.append(split[i]); } buf.append('.'); buf.append(split[split.length - 1]); try { bdMoney = new BigDecimal(buf.toString()); return moneyAsLong(bdMoney); } catch (final NumberFormatException e2) { Log.e("QifUtils", "Second parse attempt failed, falling back to rounding"); } } NumberFormat formatter = NumberFormat.getNumberInstance(); try { Number num = formatter.parse(money); BigDecimal bd = new BigDecimal(num.floatValue()); if (bd.scale() > 6) { bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); } return moneyAsLong(bd); } catch (ParseException ignored) { } Log.e("QifUtils", "Could not parse money " + money); } } return 0; }
From source file:Main.java
/** * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java *//*from ww w . j av a 2 s . c o m*/ public static long parseMoney(String money) { String sMoney = money; if (sMoney != null) { BigDecimal bdMoney; sMoney = sMoney.trim(); // to be safe try { bdMoney = new BigDecimal(sMoney); return moneyAsLong(bdMoney); } catch (NumberFormatException e) { /* there must be commas, etc in the number. Need to look for them * and remove them first, and then try BigDecimal again. If that * fails, then give up and use NumberFormat and scale it down * */ String[] split = MONEY_PREFIX_PATTERN.split(sMoney); if (split.length > 2) { StringBuilder buf = new StringBuilder(); if (sMoney.startsWith("-")) { buf.append('-'); } for (int i = 0; i < split.length - 1; i++) { buf.append(split[i]); } buf.append('.'); buf.append(split[split.length - 1]); try { bdMoney = new BigDecimal(buf.toString()); return moneyAsLong(bdMoney); } catch (final NumberFormatException e2) { Log.e("QifUtils", "Second parse attempt failed, falling back to rounding"); } } NumberFormat formatter = NumberFormat.getNumberInstance(); try { Number num = formatter.parse(sMoney); BigDecimal bd = new BigDecimal(num.floatValue()); if (bd.scale() > 6) { bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP); } return moneyAsLong(bd); } catch (ParseException ignored) { } Log.e("QifUtils", "Could not parse money " + sMoney); } } return 0; }
From source file:org.cirdles.ludwig.squid25.Utilities.java
/** * Performs excel-style rounding of double to a given number of significant * figures.//from ww w . j ava2 s . c o m * * @param value double to round * @param sigFigs count of significant digits for rounding * @return double rounded to sigFigs significant digits */ public static double roundedToSize(double value, int sigFigs) { BigDecimal valueBDtoSize = BigDecimal.ZERO; if (Double.isFinite(value)) { BigDecimal valueBD = new BigDecimal(value); int newScale = sigFigs - (valueBD.precision() - valueBD.scale()); valueBDtoSize = valueBD.setScale(newScale, RoundingMode.HALF_UP); } return valueBDtoSize.doubleValue(); }
From source file:Main.java
public static String formatAmountConversionRate(double convRate) { if (convRate == 0) return null; BigDecimal cr = new BigDecimal(convRate); int x = 7 - cr.precision() + cr.scale(); String bds = cr.movePointRight(cr.scale()).toString(); if (x > 9) bds = zeropad(bds, bds.length() + x - 9); String ret = zeropadRight(bds, 7); return Math.min(9, x) + takeFirstN(ret, 7); }
From source file:org.apache.drill.exec.planner.sql.handlers.SetOptionHandler.java
private static Object sqlLiteralToObject(final SqlLiteral literal) { final Object object = literal.getValue(); final SqlTypeName typeName = literal.getTypeName(); switch (typeName) { case DECIMAL: { final BigDecimal bigDecimal = (BigDecimal) object; if (bigDecimal.scale() == 0) { return bigDecimal.longValue(); } else {//www .j a v a 2 s . com return bigDecimal.doubleValue(); } } case DOUBLE: case FLOAT: return ((BigDecimal) object).doubleValue(); case SMALLINT: case TINYINT: case BIGINT: case INTEGER: return ((BigDecimal) object).longValue(); case VARBINARY: case VARCHAR: case CHAR: return ((NlsString) object).getValue().toString(); case BOOLEAN: return object; default: throw UserException.validationError() .message("Drill doesn't support assigning literals of type %s in SET statements.", typeName) .build(logger); } }
From source file:org.jpox.util.SQLFormat.java
/** * Formats the given BigDecimal value into a SQL floating-point literal. * BigDecimal.toString() is not well suited to this purpose because it never * uses E-notation, which causes some values with large exponents to be * output as long strings with tons of zeroes in them. *//from w w w. ja v a2 s . c om * @param bd The number to format. * * @return The formatted String. */ public static String format(BigDecimal bd) { String digits = bd.unscaledValue().abs().toString(); int scale = bd.scale(); int len = digits.length(); /* Normalize by removing any trailing zeroes. */ while (len > 1 && digits.charAt(len - 1) == '0') { --scale; --len; } if (len < digits.length()) { digits = digits.substring(0, len); } StringBuffer sb = new StringBuffer(); if (bd.signum() < 0) { sb.append('-'); } int exponent = len - scale; if (exponent < 0 || exponent > len) { /* Output in E-notation. */ sb.append('.').append(digits).append('E').append(exponent); } else if (exponent == len) { /* Output as an integer. */ sb.append(digits); } else { /* Output as "intDigits.fracDigits". */ sb.append(digits.substring(0, exponent)).append('.').append(digits.substring(exponent)); } return sb.toString(); }
From source file:com.bloatit.model.Payment.java
public static BankTransaction doPayment(final Actor<?> targetActor, final BigDecimal amount) throws UnauthorizedOperationException { if (!AuthToken.isAuthenticated()) { throw new UnauthorizedOperationException(SpecialCode.AUTHENTICATION_NEEDED); }/*from w w w . ja v a2s . c om*/ if (!targetActor.equals(AuthToken.getMember()) && !targetActor.equals(AuthToken.getAsTeam())) { } final BigDecimal amountToPay = BankTransaction.computateAmountToPay(amount); if (amountToPay.scale() > 2) { throw new BadProgrammerException("The amount to pay cannot have more than 2 digit after the '.'."); } if (amount.scale() > 2) { throw new BadProgrammerException("The amount cannot have more than 2 digit after the '.'."); } if (amount.compareTo(amountToPay) > 0) { throw new BadProgrammerException("The amount to pay must be superior to the amount '.'."); } final String orderReference = createOrderRef(targetActor); final BankTransaction bankTransaction = new BankTransaction(targetActor, // amount, // amountToPay, // orderReference); bankTransaction.setAuthorized(); return bankTransaction; }