List of usage examples for java.math BigDecimal setScale
@Deprecated(since = "9") public BigDecimal setScale(int newScale, int roundingMode)
From source file:com.scf.core.EnvTest.java
public static double halfUp(double val, int scale) { if (scale < 0) { throw new IllegalArgumentException("The scale must be a positive integer or zero"); }//from www . j a v a 2 s . co m BigDecimal b = new BigDecimal(val); //?2?? double f1 = b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); return f1; }
From source file:Main.java
/** * Formats decimal number for specified locale * * @param v Value to format//from w ww . j a v a 2 s . c o m * @param locale Locale * @return Formatted string */ public static String formatBigDecimal(BigDecimal v, Locale locale) { NumberFormat numberFormat = NumberFormat.getInstance(locale); int scale = 2; if (v.intValue() >= 100) { scale = 1; } return numberFormat.format(v.setScale(scale, RoundingMode.UP)); }
From source file:nu.mine.kino.projects.utils.Utils.java
public static Double round(Double d, int scale) { if (d == null || Double.isNaN(d)) { return d; }//from w w w . ja v a 2 s. c o m BigDecimal org = new BigDecimal(d); BigDecimal rounded = org.setScale(scale, BigDecimal.ROUND_HALF_UP); return rounded.doubleValue(); }
From source file:org.zapto.samhippiemiddlepoolchecker.Values.java
static public String valueToString(float value, int scale) { BigDecimal decimal = new BigDecimal(value); decimal = decimal.setScale(scale, BigDecimal.ROUND_HALF_UP); return decimal.toPlainString(); }
From source file:org.apache.pdfbox.rendering.TilingPaint.java
/** * Returns the closest integer which is larger than the given number. * Uses BigDecimal to avoid floating point error which would cause gaps in the tiling. *///from w w w . j a va2s .c o m private static int ceiling(double num) { BigDecimal decimal = new BigDecimal(num); decimal = decimal.setScale(5, RoundingMode.CEILING); // 5 decimal places of accuracy return decimal.intValue(); }
From source file:geogebra.util.MyMath.java
/** * Round a double to the given number of digits * @param x //from w w w. ja v a 2 s .c o m * @param digits * @return number rounded to given number of digits */ final public static double truncate(double x, int digits) { BigDecimal bd = new BigDecimal(x); bd = bd.setScale(digits, BigDecimal.ROUND_HALF_UP); return bd.doubleValue(); }
From source file:org.eyeseetea.malariacare.utils.Utils.java
public static String round(float base, int decimalPlace) { BigDecimal bd = new BigDecimal(Float.toString(base)); bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP); if (decimalPlace == 0) return Integer.toString((int) bd.floatValue()); return Float.toString(bd.floatValue()); }
From source file:com.nearinfinity.honeycomb.hbase.bulkload.FieldParser.java
private static ByteBuffer extractDecimal(String val, int precision, int right_scale) { int left_scale = precision - 2; BigDecimal x = new BigDecimal(val); boolean is_negative = x.compareTo(BigDecimal.ZERO) == -1; x = x.abs();/*from w w w . jav a 2 s.c om*/ BigDecimal left = x.setScale(0, RoundingMode.DOWN); BigDecimal right = x.subtract(left).movePointRight(right_scale); int right_bytes_len = bytesFromDigits(right_scale); int left_bytes_len = bytesFromDigits(left_scale); byte[] left_bytes = left.toBigInteger().toByteArray(); byte[] right_bytes = right.toBigInteger().toByteArray(); // Bit twiddling is fun byte[] buff = new byte[left_bytes_len + right_bytes_len]; System.arraycopy(left_bytes, 0, buff, left_bytes_len - left_bytes.length, left_bytes.length); System.arraycopy(right_bytes, 0, buff, right_bytes_len - right_bytes.length + left_bytes_len, right_bytes.length); buff[0] ^= -128; // Flip first bit, 0x80 if (is_negative) { // Flip all bits for (int i = 0; i < buff.length; i++) { buff[i] ^= -1; // 0xff } } return ByteBuffer.wrap(buff); }
From source file:Main.java
public static BigDecimal setDecimalDigit(BigDecimal number, int digit, boolean setNullIndicator) { BigDecimal returnNum = BigDecimal.ZERO; if (number != null) { if ((number.compareTo(BigDecimal.ZERO) == 0) && setNullIndicator) { return null; }//from w w w. ja v a 2 s . co m returnNum = number.setScale(digit, BigDecimal.ROUND_HALF_UP); } return returnNum; }
From source file:com.ms.app.web.commons.tools.CurrencyFormattor.java
/** * "###.####"-->"#.00" floatPrice-->formatPrice(?) *//* ww w .j a va2 s . c om*/ public static String float2formatPrice(Float price) { if (price == null) { return null; } BigDecimal b = new BigDecimal(price); float f = b.setScale(SCALE, BigDecimal.ROUND_HALF_UP).floatValue(); DecimalFormat fnum = new DecimalFormat(CURRENCY); return fnum.format(f); }