BigDecimalã®ä»çµã¿ãå©ç¨ãã¦ã¿ãã
BigDecimalãå©ç¨ããã³ã¼ããæ¸ãã¦ã¿ã¾ããã
ãã®è¨äºã§ãã£ã¦ãããã¨ã¯ãBigDecimalã®ä»çµã¿ãå³è§£ãã¦ã¿ããã§å³è§£ããå
容ã«åºã¥ãã¦ãã¾ãã
ç®æ¬¡
- åºæ¬çãªæ§é
- ã¹ã±ã¼ã«ãå¤ãã
- 足ãç®
- å¼ãç®
- æãç®
- å²ãç®
åºæ¬çãªæ§é
åºæ¬çãªæ§é ã表示ããããã®ã¡ã½ãããä½ãã¾ããã
表示形å¼ã¯ãå®éã®å¤ = æ´æ°é¨ * 10 ^ ( - ã¹ã±ã¼ã« )ãã§ãã
// BigDecimalã«ã¤ãã¦è©³ãã表示ããã public static void printBigDecimal( String msg, BigDecimal val ) { // æ´æ°é¨ãåå¾ããã BigInteger intValue = val.unscaledValue(); // ã¹ã±ã¼ã« int scale = val.scale(); // ãå®éã®å¤ = æ´æ°é¨ * 10 ^ ( - ã¹ã±ã¼ã« )ãã®å½¢å¼ã§è¡¨ç¤ºããã System.out.println( msg + ": " + val + " = " + intValue + " * 10 ^ ( - " + scale + " )" ); }
(ä¾) 1.234ã®æ§é ã表示ããã
printBigDecimal( "åºæ¬çãªæ§é ", b1 );
åºæ¬çãªæ§é : 1.234 = 1234 * 10 ^ ( - 3 )
ã¹ã±ã¼ã«ãå¤ãã
ã¹ã±ã¼ã«ãå¢ãã
(ä¾) 2.5ã®ã¹ã±ã¼ã«ã2ã«å¤ããã
b1 = new BigDecimal( "2.5" ); // ã¹ã±ã¼ã«ã2ã«å¤ããã b2 = b1.setScale( 2 );
ã¹ã±ã¼ã«å¤æ´å: 2.5 = 25 * 10 ^ ( - 1 ) ã¹ã±ã¼ã«å¤æ´å¾: 2.50 = 250 * 10 ^ ( - 2 )
ã¹ã±ã¼ã«ãæ¸ãã
(ä¾) 2.50ã®ã¹ã±ã¼ã«ã1ã«å¤ããã
b1 = new BigDecimal( "2.50" ); // ã¹ã±ã¼ã«ã1ã«å¤ããã b2 = b1.setScale( 1 );
ã¹ã±ã¼ã«å¤æ´å: 2.50 = 250 * 10 ^ ( - 2 ) ã¹ã±ã¼ã«å¤æ´å¾: 2.5 = 25 * 10 ^ ( - 1 )
ã¹ã±ã¼ã«ãæ¸ãã(丸ãå¦çãå¿ è¦)
(ä¾) 2.5ã®ã¹ã±ã¼ã«ã0ã«å¤ããã(åãæ¨ã¦)
b1 = new BigDecimal( "2.5" ); // ã¹ã±ã¼ã«ã0ã«å¤ãã(åãæ¨ã¦)ã b2 = b1.setScale( 0, BigDecimal.ROUND_DOWN );
ã¹ã±ã¼ã«å¤æ´å: 2.5 = 25 * 10 ^ ( - 1 ) ã¹ã±ã¼ã«å¤æ´å¾: 2 = 2 * 10 ^ ( - 0 )
(ä¾) 2.5ã®ã¹ã±ã¼ã«ã0ã«å¤ããã(åãä¸ã)
b1 = new BigDecimal( "2.5" ); // ã¹ã±ã¼ã«ã0ã«å¤ããã(åãä¸ã) b2 = b1.setScale( 0, BigDecimal.ROUND_UP );
ã¹ã±ã¼ã«å¤æ´å: 2.5 = 25 * 10 ^ ( - 1 ) ã¹ã±ã¼ã«å¤æ´å¾: 3 = 3 * 10 ^ ( - 0 )
(ä¾) 2.5ã®ã¹ã±ã¼ã«ã0ã«å¤ããã(丸ãã¢ã¼ãæå®æ¼ã)
b1 = new BigDecimal( "2.5" ); // ã¹ã±ã¼ã«ã0ã«å¤ããã(丸ãã¢ã¼ãæå®æ¼ã) b2 = b1.setScale( 0 );
ã¹ã±ã¼ã«å¤æ´å: 2.5 = 25 * 10 ^ ( - 1 ) java.lang.ArithmeticException: Rounding necessary at java.math.BigDecimal.divide(BigDecimal.java:1346) at java.math.BigDecimal.setScale(BigDecimal.java:2310) at java.math.BigDecimal.setScale(BigDecimal.java:2350) at BigDecimalSample.main(BigDecimalSample.java:74)
足ãç®
(ä¾) 12.3 + 0.29 = 12.59
b1 = new BigDecimal( "12.3" ); b2 = new BigDecimal( "0.29" ); // 足ãç®ããã b3 = b1.add( b2 );
足ãæ°1: 12.3 = 123 * 10 ^ ( - 1 ) 足ãæ°2: 0.29 = 29 * 10 ^ ( - 2 ) 足ãç®ã®çµæ: 12.59 = 1259 * 10 ^ ( - 2 )
å¼ãç®
(ä¾) -2.74 â 14 ï¼ -16.74
b1 = new BigDecimal( "-2.74" ); b2 = new BigDecimal( "14" ); // å¼ãç®ããã b3 = b1.subtract( b2 );
å¼ãããæ°: -2.74 = -274 * 10 ^ ( - 2 ) å¼ãæ°: 14 = 14 * 10 ^ ( - 0 ) å¼ãç®ã®çµæ: -16.74 = -1674 * 10 ^ ( - 2 )
æãç®
(ä¾) 0.012 Ã 2.3 ï¼ 0.0276
b1 = new BigDecimal( "0.012" ); b2 = new BigDecimal( "2.3" ); // æãç®ããã b3 = b1.multiply( b2 );
æããæ°1: 0.012 = 12 * 10 ^ ( - 3 ) æããæ°2: 2.3 = 23 * 10 ^ ( - 1 ) æãç®ã®çµæ: 0.0276 = 276 * 10 ^ ( - 4 )
å²ãç®
(ä¾) 1.75 ÷ 6 ï¼ 0.292 (è¨ç®çµæã®ã¹ã±ã¼ã«ã¯3ã丸ãã¢ã¼ãã¯åãä¸ã)
b1 = new BigDecimal( "1.75" ); b2 = new BigDecimal( "6" ); // å²ãç®ããã(è¨ç®çµæã®ã¹ã±ã¼ã«ã¯3ã丸ãã¢ã¼ãã¯åãä¸ã) b3 = b1.divide( b2, 3, BigDecimal.ROUND_UP );
å²ãããæ°: 1.75 = 175 * 10 ^ ( - 2 ) å²ãæ°: 6 = 6 * 10 ^ ( - 0 ) å²ãç®ã®çµæ: 0.292 = 292 * 10 ^ ( - 3 )
(ä¾) 1.75 ÷ 6 ï¼ 0.291 (è¨ç®çµæã®ã¹ã±ã¼ã«ã¯3ã丸ãã¢ã¼ãã¯åãæ¨ã¦)
b1 = new BigDecimal( "1.75" ); b2 = new BigDecimal( "6" ); // å²ãç®ããã(è¨ç®çµæã®ã¹ã±ã¼ã«ã¯3ã丸ãã¢ã¼ãã¯åãæ¨ã¦) b3 = b1.divide( b2, 3, BigDecimal.ROUND_DOWN );
å²ãããæ°: 1.75 = 175 * 10 ^ ( - 2 ) å²ãæ°: 6 = 6 * 10 ^ ( - 0 ) å²ãç®ã®çµæ: 0.291 = 291 * 10 ^ ( - 3 )
(ä¾) 1.75 ÷ 6 ï¼ ã¨ã©ã¼ (è¨ç®çµæã®ã¹ã±ã¼ã«ã¯3ã丸ãã¢ã¼ãã¯ä¸¸ãä¸è¦)
b1 = new BigDecimal( "1.75" ); b2 = new BigDecimal( "6" ); // å²ãç®ããã(è¨ç®çµæã®ã¹ã±ã¼ã«ã¯3ã丸ãã¢ã¼ãã¯ä¸¸ãä¸è¦) b3 = b1.divide( b2, 3, BigDecimal.ROUND_UNNECESSARY );
å²ãããæ°: 1.75 = 175 * 10 ^ ( - 2 ) å²ãæ°: 6 = 6 * 10 ^ ( - 0 ) java.lang.ArithmeticException: Rounding necessary at java.math.BigDecimal.divide(BigDecimal.java:1346) at BigDecimalSample.main(BigDecimalSample.java:162)
(ä¾) 1.75 ÷ 6 ï¼ 0.30 (è¨ç®çµæã®ã¹ã±ã¼ã«ã¯ãå²ãããæ°ã®ã¹ã±ã¼ã«ãã¨åãã丸ãã¢ã¼ãã¯åãä¸ã)
b1 = new BigDecimal( "1.75" ); b2 = new BigDecimal( "6" ); // å²ãç®ããã(è¨ç®çµæã®ã¹ã±ã¼ã«ã¯ãå²ãããæ°ã®ã¹ã±ã¼ã«ãã¨åãã丸ãã¢ã¼ãã¯åãä¸ã) b3 = b1.divide( b2, BigDecimal.ROUND_UP );
å²ãããæ°: 1.75 = 175 * 10 ^ ( - 2 ) å²ãæ°: 6 = 6 * 10 ^ ( - 0 ) å²ãç®ã®çµæ: 0.30 = 30 * 10 ^ ( - 2 )
(ä¾) 1.75 ÷ 6 ï¼ 0.29 (è¨ç®çµæã®ã¹ã±ã¼ã«ã¯ãå²ãããæ°ã®ã¹ã±ã¼ã«ãã¨åãã丸ãã¢ã¼ãã¯åãæ¨ã¦)
b1 = new BigDecimal( "1.75" ); b2 = new BigDecimal( "6" ); // å²ãç®ããã(è¨ç®çµæã®ã¹ã±ã¼ã«ã¯ãå²ãããæ°ã®ã¹ã±ã¼ã«ãã¨åãã丸ãã¢ã¼ãã¯åãæ¨ã¦) b3 = b1.divide( b2, BigDecimal.ROUND_DOWN );
å²ãããæ°: 1.75 = 175 * 10 ^ ( - 2 ) å²ãæ°: 6 = 6 * 10 ^ ( - 0 ) å²ãç®ã®çµæ: 0.29 = 29 * 10 ^ ( - 2 )