List of usage examples for java.math BigDecimal BigDecimal
public BigDecimal(long val)
From source file:Main.java
/** * Adopted from http://jgnash.svn.sourceforge.net/viewvc/jgnash/jgnash2/trunk/src/jgnash/imports/qif/QifUtils.java *//*w ww . j a va2s .c om*/ 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:Main.java
/** * Parses the supplied decimal/floating point string and returns its value. * /* ww w .j a v a 2 s . c om*/ * @param s * A string representation of an xsd:decimal or xsd:double value. * @return The decimal/floating point value represented by the supplied string argument. * @throws NumberFormatException * If the supplied string is not a valid xsd:decimal or xsd:double value. */ public static BigDecimal parseDecimal(String s) { // Note: BigDecimal can handle leading plus signs itself return new BigDecimal(s); }
From source file:Main.java
/** * Write a {@link BigDecimal} value into XML output * with a valid latitude range.// w w w . j a v a 2 s . c om * * @param value * value to write * * @return * XML string * * @throws IllegalArgumentException * if a validation error occured */ public static String printLatitude(BigDecimal value) { return printDecimal(value, new BigDecimal("-90"), new BigDecimal("90"), 10, false); }
From source file:Main.java
/** * Write a {@link BigDecimal} value into XML output * with a valid longitude range./*from www . j a va 2 s . c o m*/ * * @param value * value to write * * @return * XML string * * @throws IllegalArgumentException * if a validation error occured */ public static String printLongitude(BigDecimal value) { return printDecimal(value, new BigDecimal("-180"), new BigDecimal("180"), 10, false); }
From source file:Main.java
public static double add(double value1, double value2) { BigDecimal b1 = new BigDecimal(value1); BigDecimal b2 = new BigDecimal(value2); return b1.add(b2).doubleValue(); }
From source file:Main.java
public static double mul(double value1, double value2) { BigDecimal b1 = new BigDecimal(value1); BigDecimal b2 = new BigDecimal(value2); return b1.multiply(b2).doubleValue(); }
From source file:Main.java
public static double sub(double value1, double value2) { BigDecimal b1 = new BigDecimal(value1); BigDecimal b2 = new BigDecimal(value2); return b1.subtract(b2).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:Main.java
/** * Write a {@link BigDecimal} value into XML output, * that matches the 'NumberOfRoomsType' simple type. * * @param value// www . j a v a 2 s . c o m * value to write * * @return * XML string * * @throws IllegalArgumentException * if a validation error occured */ public static String printRoomNr(BigDecimal value) { return printDecimal(value, new BigDecimal("0.49"), BigDecimal.TEN.pow(4), 2, false); }
From source file:be.error.rpi.ebus.Support.java
public static String addTemperatureToCommand(String command, BigDecimal temperature, int offset) { String converted = encodeDATA2c(temperature.add(new BigDecimal(offset))); return format(command, substring(converted, 2, 4), substring(converted, 0, 2)); }