package elte.java2_utikalauz5.math; import java.math.*; /** BigDecimal használata. @link.forrásfájl {@docRoot}/../data/math/src BigDec.java @link.letöltés {@docRoot}/../data/math BigDec.jar @since Java 2 Útikalauz programozóknak */ class BigDec{ private static MathContext mc = new MathContext(2, RoundingMode.HALF_UP); static void round(String num, int scale, int roundingMode) { try { System.out.println(new BigDecimal(num).setScale(scale, roundingMode)); } catch (Exception e) { System.out.println(e); } } public static void main(String[] args) { System.out.println(new BigDecimal(1.23)); // 1.229999999999999982236431605997495353221893310546875 try { System.out.println(new BigDecimal(Double.MAX_VALUE * Double.MAX_VALUE)); } catch (Exception e) { System.out.println(e); // NumberFormatException } System.out.println(new BigDecimal("-123.456"));//-123.456 System.out.println(new BigDecimal("123.")); // 123 System.out.println(new BigDecimal(".123")); // 0.123 System.out.println(new BigDecimal(".0")); // 0.0 System.out.println(new BigDecimal("0.")); // 0 try { System.out.println(new BigDecimal(" 123.456")); } catch (Exception e) { System.out.println(e); // NumberFormatException } try { System.out.println(new BigDecimal("123.456 ")); } catch (Exception e) { System.out.println(e); // NumberFormatException } System.out.println(new BigDecimal("+123.456")); // 123.456 try { System.out.println(new BigDecimal(".")); } catch (Exception e) { System.out.println(e); // NumberFormatException } System.out.println(new BigDecimal(new BigInteger("123"), 2)); // 1.23 BigDecimal a = new BigDecimal("-123.45"); BigDecimal b = new BigDecimal("123.45"); System.out.println(a.negate().equals(b)); // true a = new BigDecimal("1.23"); b = new BigDecimal("1.230"); System.out.println(a.equals(b)); // false System.out.println(a.compareTo(b)); // 0 az, mint stringek egyenlőek a = new BigDecimal("1.25"); b = new BigDecimal("0.25"); /** lebeg\u0151pontos számítás - J2SE 5.0 */ BigDecimal c = a.multiply(b, mc); System.out.println("Lebeg\u0151pontos számolás: "+c); /** pontos érték, majd skálázás - J2SE 2.0 */ c = a.multiply(b); System.out.println("Pontos érték: "+c); c = c.setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println("Skálázó faktor beállítása után: "+c); a = new BigDecimal("2"); b = new BigDecimal("3"); MathContext mc1 = new MathContext(7, RoundingMode.HALF_EVEN); System.out.println( a.divide(b, mc1) ); /** Az eredmény: 0.6666667*/ MathContext mc2 = new MathContext(7, RoundingMode.DOWN); System.out.println( a.divide(b, mc2) );/** Az eredmény: 0.6666666*/ a = new BigDecimal("10.00"); System.out.println(a.divide(new BigDecimal("5"), BigDecimal.ROUND_UNNECESSARY)); // Az eredmény : 2.00 try { System.out.println(a.divide(new BigDecimal("3"), BigDecimal.ROUND_UNNECESSARY)); } catch (Exception e) { System.out.println(e); // ArithmeticException } System.out.println(a.divide(new BigDecimal("3"), BigDecimal.ROUND_DOWN)); // Az eredmény : 3.33 System.out.println(a.divide(new BigDecimal("3"), 10, BigDecimal.ROUND_DOWN)); // Az eredmény : 3.3333333333 System.out.println(a.divide(new BigDecimal("3"), 10, BigDecimal.ROUND_CEILING));// Az eredmény : 3.3333333334 a = new BigDecimal("1.23"); System.out.println(a.setScale(3)); // 1.230 // Skálázó faktor csökkentése round(".5", 0, BigDecimal.ROUND_CEILING); // 1 round(".5", 0, BigDecimal.ROUND_DOWN); // 0 round(".5", 0, BigDecimal.ROUND_FLOOR); // 0 round(".5", 0, BigDecimal.ROUND_HALF_DOWN); // 0 round(".5", 0, BigDecimal.ROUND_HALF_EVEN); // 0 round(".5", 0, BigDecimal.ROUND_HALF_UP); // 1 round(".4", 0, BigDecimal.ROUND_HALF_UP); // 0 round(".4", 0, BigDecimal.ROUND_UP); // 1 round(".5", 0, BigDecimal.ROUND_UNNECESSARY); // ArithmeticException round("1.0", 0, BigDecimal.ROUND_UNNECESSARY); // 1 round("-.5", 0, BigDecimal.ROUND_CEILING); // 0 round("-.5", 0, BigDecimal.ROUND_DOWN); // 0 round("-.5", 0, BigDecimal.ROUND_FLOOR); // -1 round("-.5", 0, BigDecimal.ROUND_HALF_DOWN); // 0 round("-.5", 0, BigDecimal.ROUND_HALF_EVEN); // 0 round("-.5", 0, BigDecimal.ROUND_HALF_UP); // -1 round("-.4", 0, BigDecimal.ROUND_HALF_UP); // 0 round("-.4", 0, BigDecimal.ROUND_UP); // -1 round("-.5", 0, BigDecimal.ROUND_UNNECESSARY); // ArithmeticException round("-1.0", 0, BigDecimal.ROUND_UNNECESSARY); // -1 a = new BigDecimal("1.00"); System.out.println(a.scale()); // 2 a = a.movePointLeft(1); System.out.println(a); // 0.100 System.out.println(a.scale()); // 3 a = a.movePointLeft(-2); System.out.println(a); // 10.0 System.out.println(a.scale()); // 1 a = new BigDecimal("1.0"); System.out.println(a.scale()); // skála értéke 1 a = a.movePointRight(2); System.out.println(a); // 100 System.out.println(a.scale()); // skála érétk 0 a = a.movePointRight(-3); System.out.println(a); // .100 System.out.println(a.scale()); // skála értéke 3 } }