Java Math Operations

type declarations: double myDoubleDeclaration = 288.15; Double myDoubleObjectDeclaration = 288.15; Addition +: x += 1; x++; önce artırma işlemi yapılır daha sonra diğer işlemlere geçilir. ++x; önce diğer işlemler yapılır sonra artırım işlemi yapılır. x = x + 1; subtraction -: multiplication *: Division /: 1/3 = 0.0 (double) (1/3) = 0.0 (double) 1/3 = 0.3333333333333333 Modulo/Remainder %: 17%7=3 17%7 means once 17(numerator) divided by 7(denominator) the result/remainder will be 3 18%6=0 Power Multipliers: Square ^2: Math.pow(i, 2); Math.pow(i, 3); Decimal digit double with two digits after the decimal point double val = 200.3456; String result = String.format(“%.2f”, val); // 200.35 * DecimalFormat df = new DecimalFormat(“####0.00”); df.format(val); // 200.35 * DecimalFormat df = new DecimalFormat(“#.##”); Double.valueOf(df.format(val)); // 200.35 * DecimalFormat df = new DecimalFormat(“0.00”); df.format(val); * val = val * 100; // 20034.56 val = Math.round(val); // 20035 val = val /100; // 200.35 * Apache commons math: Precision.round(val, 2); // 200.35 * new BigDecimal(val.toString()).setScale(2, RoundingMode.HALF_UP).doubleValue(); * String str = String.format(“%1.2f”, val); val = Double.valueOf(str); * val = Math.floor(val * 100) / 100; * val02 = val * 100 // 20034.56 val03 = (int) val02; // 20034 val = val03 / 100; // 200.34 Float or Double Numbers to String double val = 200.3456; String doubleStr = String.format(“%.2f”, val); // 200.35 * String doubleStr = String.format(“%.2f”, 1.9999); // 2.00 * DecimalFormat df = new DecimalFormat(“#.##”); String formatted = df.format(val); System.out.println(formatted); // 200.35 * String strDouble = String.format(“%.2f”, 1.00023); System.out.println(strDouble); // 1.00 * DecimalFormat df = new DecimalFormat(“#.##”); String formatted = df.format(1.00023); System.out.println(formatted); // 2 * NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(3); System.out.println(nf.format(val)); // 200 * NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(5); System.out.println(nf.format(round)); // 200.35 * * BigDecimal Definitions: BigDecimal definition: BigDecimal myBigDecimalVal = new BigDecimal(“4.41”); BigDecimal myBigDecimalVal = new BigDecimal(1.03); BigDecimal myBigDecimalVal = new BigDecimal(“1.03”); Operations: with BigDecimal: BigDecimal myBigDecimalVal = BigDecimal.ZERO; // 2 BigDecimal myBigDecimalVal = BigDecimal.ZERO.setScale(2, RoundingMode.CEILING); // 2,70 BigDecimal myBigDecimalVal = new BigDecimal(new BigDecimal(“1”).doubleValue() * new BigDecimal(“2”).doubleValue()); BigDecimal myBigDecimalVal1 = BigDecimal.valueOf(new BigDecimal(“1”).doubleValue() + myDoubleObjectDeclaration * new BigDecimal(“2”).doubleValue()); BigDecimal myBigDecimalVal2 = BigDecimal.valueOf(226.29 / (99.15 + 211.9)); BigDecimal myBigDecimalVal3 = BigDecimal.valueOf((1.8 * new BigDecimal(“3”).doubleValue() + 492) * new BigDecimal(“4”).doubleValue() – 460); BigDecimal myBigDecimalVal4 = BigDecimal.valueOf(0.0330378 * (Math.pow(new BigDecimal(“5”).doubleValue(), -2))); BigDecimal myBigDecimalVal5 = BigDecimal.valueOf(Math.pow(Math.pow(new BigDecimal(“6”).doubleValue(), 0.5),((double)1/3))); double $C$4 = 273.15; BigDecimal myBigDecimalVal6 = BigDecimal.valueOf(new BigDecimal(“7”).doubleValue()+$C$4); Calculate without tax value, BigDecimal to String with 2 digit: discountAmountWithTax.divide(new BigDecimal(1.06), 2).toString(); Object to BigDecimal: private BigDecimal getBigDecimalValue(Object obj) { if (obj != null) { if (obj instanceof BigDecimal) { return ((BigDecimal) obj).setScale(2, RoundingMode.HALF_UP); } } return null; } Compare BigDecimal is greater than zero: (new BigDecimal(dto.getSavings()).compareTo(BigDecimal.ZERO) > 0) if (value.compareTo(BigDecimal.ZERO) > 0) For more information you can refer to these pages and sites: Float and Float mathematics operations tutorials.jenkov.com /java/math-operators-and-math-class.html#division javatpoint.com /java-math]]>

Leave a Reply

Your email address will not be published. Required fields are marked *