Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 2
Javaで小数点の切り上げと切り捨て | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 17
Javaで小数点の切り上げと切り捨て
数値計算する時に小数点~~桁で切り上げとか切り捨てとかするがよくあります。
javaで切り上げをするにはBigDecimal型で数値計算し、 divide メソッドを使用して 1 で割ります。
その時、引数で小数点の切り上げの桁数や切り上げか切り捨てかを指定します。

ちなみに切り上げとは、小数点1桁で切り上げといった場合、4.1なら5になります。
数字がマイナスの場合の切り上げも同様で、-4.1なら-5となります。
-16.44を小数点一桁で切り上げる場合、-16.5となります。逆に切り捨てなら-16.4となります。
以下は、小数点1桁で切り上げの例です。

public class Kiriage {
    public static void main(String[] args) {
        BigDecimal c = new BigDecimal("-4.11");
        BigDecimal b = new BigDecimal("4");
        b = c.multiply(b);
        BigDecimal a = b.divide(new BigDecimal("1"),0,BigDecimal.ROUND_UP);

        System.out.println("切り上げ前" + b + "¥n切り上げ後"+a);
    }
}

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 73

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 73

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 82

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/bigdecimal/round/round.html on line 82